Tuesday, 10 October 2017

write a program to calculate sum of natural numbers

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<iostream>
#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();
}

Output
----------------
Please enter  any number
4

sum od natural numbers=10

-------------------------

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();
}


Output
----------------
Please enter  any number
4

sum od natural numbers=10
-------------------------
You should have basic programming knowledege
like syntax and variable and if else statement or for loop that is must learn before starting
the program.
The people who want to learn programming language.this website very beneficial for those people.

No comments:

Post a Comment