General code 2

CODE NO:- 14

Q) Write a program to calculate the sum of n natural numbers.

#LOGIC
Here we need to use the mathematical formula for the sum of n natural numbers i.e (n*(n+1)) /2.

#Program( In C++):

In  C++ language we use  ' // '  for Single line comment and /*---*/ for multi line comment

#include<iostream> 
using namespace std;
int main()
{
int n,re;
cout<<"\n enter the val of n upto which we want the sum:";
cin>>n;
re=(n*(n+1))/2;
cout<<"\n result is:"<<re;
return 0;
}

We got this output by using DEV C++ (IDE)



Link: For other General Problems

Comments

Post a Comment

Popular posts from this blog

WHY PROGRAMMING?

Series code 2

Pattern Printing coding 7