C++ Programs to Add All Numbers up to n using For Loop

Write C++ Programs to Add All Numbers up to n using For Loop

// CPP Programs to Add All Numbers up to n using For Loop

#include <iostream>

using namespace std;

int main()
{
	int n, sum = 0;

	cout << "Enter Number :--> ";
	cin >> n;

	int i = 1;

	cout << "Sum of First " << n << " Number :--> ";

        for(i=1; i<=n; i++)
	{
		sum = sum + i;
	}
	cout << sum;
	return 0;

}

Output:

Enter Number :--> 5
Sum of First 5 Number :--> 15

≪ Previous | Next ≫