#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
codeaft@codeaft:~$ g++ codeaft.cpp codeaft@codeaft:~$ ./a.out
———————————————————————————————————————————
Program to print the addition of N numbers
———————————————————————————————————————————
Enter the number 10
The sum of first 10 natural numbers is = 55
———————————————————————————————————————————
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.