Sum of n natural no. using {[n(n+1)]/2} || Arithmetic Programs || C++
In this, we are going to see a program for sum of n natural no. using {[n(n+1)]/2} in C++ Programming Language.
 

Eg :
sum of first 5 natural numbers (5*6)/2 = 15

The Code given below can be used in TURBO C++ Compilers: -


#include <iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    int n, sum;
    cout << "Total number of n terms?\n";
    cin >> n;
    sum = (n * (n + 1)) / 2;
    cout << "Sum of " << n << " terms are:- " << sum;

    getch();
}

//.............Coded by YASH ALAPURIA

The Code given below can be used in gcc/g++ Compilers: -


#include <iostream>
using namespace std;

int main()
{
    int n, sum;
    cout << "Total number of n terms?\n";
    cin >> n;
    sum = (n * (n + 1)) / 2;
    cout << "Sum of " << n << " terms are:- " << sum;

    return 0;
}

//.............Coded by YASH ALAPURIA


#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post