C++ Program To Calculate Sum Of Natural Numbers - Programiz
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 = 1275This 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.
Từ khóa » C++ 1+2+3+...+n
-
C++ Program To Find Sum Of Series 1 + 2 + 3 +......+ N
-
C++ : Calculate The Series (1) + (1+2) + … + (1+2+3+...+n)
-
C++ Add 1+2+3+...+N - Stack Overflow
-
Find Sum Of Series 1 + 2 + 3 +......+ N - C++ Program | Code With C
-
Sum Of The Series 1^1 + 2^2 + 3^3 + ... + N^n Using Recursion In C++
-
C++ Program To Find The Sum Of A Series 1/1! + 2/2! + 3/3! + 4/4! + ...
-
Tính S(n) = 1 + 2 + 3 + … + N Bằng C / C++
-
Program To Find Sum Of Series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
-
Program To Find The Sum Of The Series (1/a + 2/a^2 + 3/a^3 + ... + N/a^n)
-
How To Write This Program In C++ - Quora
-
An Introduction To C++ Programming For First-time Programmers
-
Program To Calculate The Series (1) + (1+2) + (1+2+3) + ... - StudyMite
-
C Program To Find Sum Of Series 1^3 + 2^3 + 3^3 + … + N^3
-
Two Sum - LeetCode