logo image

C++ program to print sum of n natural numbers

35


Following program shows you how to print sum of n natural numbers. In this program we get number from user and shows sum of n natural numbers using following formula Sum of n natural numbers = input X (input + 1)/2

#include <iostream>

int main() {
	int input;
	std::cout << "Enter a number:";
	std::cin >> input;
	int result = input * (input + 1) / 2;
	std::cout << "Sum of n natural numbers is: " << result;
}

Output:

Enter a number: 12
Sum of n natural numbers is: 78