C++ for loop

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>
using namespace 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
Last edited on
Well, it works, it does what advertised, it does not contain blatant mistakes, so it is fine.
at the end you should have a retrun 0; under the cout << "The sum ...
Last edited on
Also I may be misreading but I think it should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.