C++ Program to Find Sum of n Numbers Using For Loop

C++ Program to add N numbers. This program will print the set of multiple number to add in a single program in c++ programming language.

#include <iostream>
using namespace std;
int main()
{
int i,n, sum = 0;
cout << "Enter the number";
cin >> n;
for (i = 1; i <= n; ++i)
{
sum += i;
}
cout << "Sum = " << sum;
return 0;
}

 

Output:

Enter the number 44

Sum = 990

For Loop Definition in C++

A loop is used to continually execute a set of statements until a certain condition is met. When showing numbers from 1 to 100, 

for example, you might wish to set the value of a variable to 1 and display it 100 times, raising its value by 1 with each loop iteration.

Return Statement in C++

A return statement ends the execution of a function and returns control to the calling function. 

Execution resumes in the caller function immediately following the call. 

A return statement has the ability to return a value to the function that called it.

Namespace in C++

A namespace is a declarative region that provides a scope to the names of types, functions, and variables contained within it.

Namespaces are used to organise code into logical groups and to avoid name collisions, which can occur when your code base contains multiple libraries.