Write a C++ program to calculate the sum of first n natural number, where n is entered by user. 

Program Code:
#include <iostream>
using namespace std;
int main()

int i;   
int number, sum = 0;   
cout << "Enter a positive integer to calculate the sum is : ";   
cin >> number;  
for (i = 1; i <= number; i++) 
{      
sum = sum + i;  
}   
cout << " Sum of first n natural number = " ;
cout<< sum;   
return 0;
}

The program output is tested on www.jdoodle.com

Output:
Enter a positive integer to calculate the sum is : 7
Sum of first n natural number = 28


Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: