Wednesday, 16 December 2015

program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50.  

#include <iostream>
using namespace std;
int main()
{
int n,sum=0;
cout <<"please enter the number you want sum\n";
cin >> n;
for (int i = 1; i <= n ; ++i) {
sum = sum+i;
}
cout<< "sum till number is" <<sum;
return 0;

}

No comments:

Post a Comment