Monday, November 14, 2011

Sum of Natural Numbers

Wap to print the sum of first n natural numbers.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,sum=0,n;
cout<<"no. of natural numbers";
cin>>n;
for(i=1;i<=n;++i)
{
cout<<"\n"<<i;
sum+=i;
}
cout<<"\n"<<" sum of first"<<n<<"natural number is"<<sum<<"\n";
getch();
}

****************************
OUTPUT:
no. of natural numbers 5
1
2
3
4
5
sum of first 5 natural number is 15

****************************

No comments:

Post a Comment