#include<iostream>usingnamespacestd;intmain(){intn,sum;cout<<"———————————————————————————————————————————";cout<<"\nProgram to print the addition of N numbers";cout<<"\n———————————————————————————————————————————";cout<<"\nEnter the number ";cin>>n;if(n>=0){sum=(n*(n+1)/2);cout<<"\nThe sum of first "<<n<<" natural numbers is = "<<sum;}else{cout<<"\nPlease enter +ve number ";}cout<<"\n———————————————————————————————————————————\n";return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Program to print the addition of N numbers
———————————————————————————————————————————
Enter the number 10
The sum of first 10 natural numbers is = 55
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.