write a program to find sum of n numbers

A number n is taken from user.
Sum of all natural numbers upto n is displayed.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<"Please enter a number: ";
  8. cin>>n;
  9. //Coding by: Snehil Khanor
  10. //http://WapCPP.blogspot.com
  11. for (int i=1;i<=n;i++)
  12. sum=sum+i;
  13. cout<<sum;

  14. getch();
  15. }

Check out this stream