C++ program to print the addition of N numbers
kw.cpp
#include <iostream>
using namespace std;
int main()
{
    int n, 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";
    return 0;
}
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:~$
Advertisement