Write a C program that asks the user to input an integer N u

Write a C++ program that asks the user to input an integer N (use the cin command), Calculates the sum sigma_j = 1^N j (using either a while or for loop); And prints out the answer to the command window with some text (e.g. \"The sum is 6\") using the cout command.

Solution

#include <iostream>

using namespace std;

int main()
{
int n;
int sum=0;
/* print Enter integer number N\"*/
cout << \"Enter integer number N :\" ;
cin>>n;

/*for loop to add sum up to n*/
for(int i=1;i<=n;i++)
{
sum=sum+i;
}

/*print total sum*/
cout<<\"The sum is : \"<<sum<<endl;

return 0;
}

--------------------

output sample 1:-

Enter integer number N :4
The sum is : 10

--------------------

output sample 2:-

Enter integer number N :6
The sum is : 21

------------------

output sample 3:-

Enter integer number N :3
The sum is : 6

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.

 Write a C++ program that asks the user to input an integer N (use the cin command), Calculates the sum sigma_j = 1^N j (using either a while or for loop); And

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site