Question #13883

c++ program (using while loop)that input a number and output the sum from 1 up to the input value ?

Expert's answer

#include<iostream>
int main()
{
int n;
std::cout <<
"Enter n: ";
std::cin >> n;

int s=0;
for (int i=1; i<=n;
i++)
{
s+=i;
};
std::cout << "Sum of numbers from 1 to "
<< n << " is " << s << std::endl;
return
0;
};
LATEST TUTORIALS
APPROVED BY CLIENTS