0% found this document useful (0 votes)
32 views1 page

4 C++ Program To Calculate Sum of Natural Numbers

Uploaded by

Muluken Dejen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views1 page

4 C++ Program To Calculate Sum of Natural Numbers

Uploaded by

Muluken Dejen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Search tutorials and examples

C++ Program to Calculate Sum of


Natural Numbers
In this example, you'll learn to calculate the sum of natural numbers.

A DV E RT I S E M E N T S

To understand this example, you should have the knowledge of the following C++
programming topics:

C++ for Loop

Positive integers 1, 2, 3, 4... are known as natural numbers.

This program takes a positive integer from user( suppose user entered n ) then,
this program displays the value of 1+2+3+....+n.

Example: Sum of Natural Numbers using loop

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= n; ++i) {


sum += i;
}

cout << "Sum = " << sum;


return 0;
}

Output

Enter a positive integer: 50


Sum = 1275

A DV E RT I S E M E N T S

This program assumes that user always enters positive number.

If user enters negative number, Sum = 0 is displayed and program is terminated.

This program can also be done using recursion. Check out this article for
calculating sum of natural numbers using recursion.

Share on: Was this article helpful?

A DV E RT I S E M E N T S

Related Examples

C++ Example C++ Example

Find Sum of Natural Numbers using Print Number Entered by User


Recursion

C++ Example C++ Example

Calculate Average of Numbers Using Calculate Factorial of a Number Using


Arrays Recursion

Tutorials Examples
Join our newsle er for the latest Python 3 Tutorial Python Examples
updates.
JavaScript Tutorial JavaScript Examples

Enter Email Address* Join C Tutorial C Examples

Java Tutorial Java Examples

Kotlin Tutorial Kotlin Examples

C++ Tutorial C++ Examples

Swi Tutorial

C# Tutorial

DSA Tutorial

Company Apps

About Learn Python

Advertising Learn C Programming

Privacy Policy Learn Java

Terms & Conditions

Contact

Blog

Youtube

© Parewa Labs Pvt. Ltd. All rights reserved.

You might also like