C++ Program to Add First N Numbers using While Loop

Write C++ Program to Add First N Numbers using While Loop

// CPP Program to Add First N Numbers using While Loop

#include <iostream>

using namespace std;

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

	cout << "Enter the number :--> ";
	cin >> n;

	int i = 1;

	cout << "The sum of first " << n << " number is :--> ";

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

}

Output:

Enter the number :--> 5
The sum of first 5 number are :--> 15

≪ Previous | Next ≫