Â
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