Write a program to display the sum added backwards of 1 2

Write a program to display the sum added backwards of 1 + 2 + 3 + .. + 9 + 10 like below:

Sum of 10 + 9 + .. + 1 is 10 in iteration 1

Sum of 10 + 9 + .. + 1 is 19 in iteration 2

….

Sum of 10 + 9 + .. + 1 is 54 in iteration 9

Sum of 10 + 9 + .. + 1 is 55 in iteration 10

(a) Do this in C++

(b) Do this in C#

Solution

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   int i=1;
   int sum = 0;
  
   for(i=1;i<=10;i++)
   {
   sum = sum+10-i+1;
   cout << \"Sum of 10 + 9 + .. + 1 is \" << sum << \" in iteration \" <<i << endl;
   }
  
   return 0;
}

using System;

public class Test
{
   public static void Main()
   {
       // your code goes here
   int i=1;
   int sum = 0;
  
   for(i=1;i<=10;i++)
   {
   sum = sum+10-i+1;
   Console.WriteLine(\"Sum of 10 + 9 + .. + 1 is \" +sum +\" in iteration \"+i);
   }
  
   }
}

Write a program to display the sum added backwards of 1 + 2 + 3 + .. + 9 + 10 like below: Sum of 10 + 9 + .. + 1 is 10 in iteration 1 Sum of 10 + 9 + .. + 1 is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site