 Write a program in C++ to calculate the sum of natural numbers.
 Write a C++ program to find sum of numbers between 1 to N.
 WAP in C++ to find sum of consecutive numbers using for loop.
Natural Numbers are counting numbers, that is 1, 2, 3, 4, 5... etc. Remember, zero and negative numbers are not part of natural numbers. In this program, we will take a positive number N as input from user and find the sum of all natural numbers between 1 to N(including 1 and N).
Algorithm to calculate sum of natural numbers
 Take number of elements(let it be N) as input from user using cin.
 Using a for loop, read N number from user and add them to a sum variable.
 Print the sum of all numbers using cout.
C++ program o find sum of all numbers between 1 to N
/* * C++ Program to find sum of N Natural Numbers */ #include <iostream> using namespace std; int main(){ int N=0, num, i, sum=0; cout << "Enter the number of integers to add"; cin >> N; cout << "Enter " << N << " numbers seperated by space\n"; for(i= 0; i< N; i++){ cin >> num; sum = sum + num; } cout << "SUM = " << sum; return 0; }Output
Enter the number of integers to add: 6 Enter 5 numbers seperated by space 1 2 3 4 5 6 SUM = 21
Related Topics
