C++ Program to calculate sum of natural numbers
In this programe we learn you how to calculate natural number.
You must have basic knowledge of C++ programming language and variable , while loop and if statement
c++ is give permission the user to enter any number.we calculate the sum of natural number through for loop and while loop
what is natural number?
A positive number like as 1, 2, 3, 4, 5, 6, 7, 8 ,9 it is also known as natural number.
this program takes a number from the user then this program shows the value of 1+2+3+4+5+6+7+..+n.
Example1: Sum of Natural Numbers using loop
#include<conio.h>
using namespace std;
int main()
{
int Num, j, Sum = 0;
cout>>"\n Please Enter any Number Value\n";
cin<<Number;
for(j = 1; j <= Num; j++)
{
Sum = Sum + j;
}
cout>>"Sum of Natural Numbers =">>Sum;
return 0;
getch();
}
Example 2: Sum of Natural Numbers using while loop
#include<iostream>#include<conio.h>
using namespace std;
int main()
{
int num, j=1, Sum = 0;
cout>>"\n Please Enter any Number Value\n";
cin<<num;
while(j <=num)
{
Sum = Sum + j;
}
cout>>"Sum of Natural Numbers =">>Sum;
return 0;
getch();
}