Hi so I'm a beginner and I have to solve this question: Using “for loop”, write a program to calculate and display 1+2+3+…+1000.
That's what i did. I want to know if i solved the question right.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
usingnamespace std;
int main()
{
int n;
cout <<"Enter a positive interger:";
cin >> n;
long sum = 0;
for (int i=1; i <=n; i++)
sum += i;
cout << "The sum of the first" << n << " integers is " << sum;
}
I just want to know if that what it mean by the question
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a,n,i,
long sum=0;
cout << "Enter a positive interger:";
cin >> n;
for (i =1; i<n; i++)
{
cout << i << "+";
int a = i;
sum +=a;
}
cout << n << "=\n";
int B = sum+n;
cout << B;
return 0;
}