Write a program in C++ to read a number n and find out the sum of the integers from 1 to
then from 1 to 3, then 1 to 4, and so on and to display the sum of the integers from 1 to
n.
For example, from
1 to 2 = 1
1 to 3 = 3
1 to 4 = 6
1 to 5 = 10
1 to 6 = 18
#include <iostream>
using namespace std;
//Summ out 1 -2 1-3 and out to display 1-n
int main() {
int n;
cout<<"Please enter n: ";
cin>>n;
int sum=1;
for(unsigned i=2;i<=n;i++)
{
cout<<1<<"-"<<i<<" ="<<sum<<endl;
sum+=i;
}
return 0;
}
Comments
Leave a comment