write a cpp program to calculate sum of first n natural numbers

Solutions on MaxInterview for write a cpp program to calculate sum of first n natural numbers by the best coders in the world

showing results for - "write a cpp program to calculate sum of first n natural numbers"
Emely
20 Jan 2017
1// Method 1: Mathematical -> Sum up numbers from 1 to n 
2int sum(int n){
3	return (n * (n+1)) / 2;
4}
5
6// Method 2: Using a for-loop -> Sum up numbers from 1 to n 
7int sum(int n){
8	int tempSum = 0; 
9  	for (int i = n; i > 0; i--){
10     	tempSum += i;  
11    }
12  	return tempSum; 
13}
14
15// Method 3: Using recursion -> Sum up numbers from 1 to n 
16int sum(int n){
17	return n > 0 ? n + sum(n-1) : 0; 
18}
Victor
26 Aug 2018
1
2#include <iostream>
3
4using namespace std;
5
6int main(){
7	int N,i,sum=0;
8	cin>>N;
9     for(i=0;i<=N;)
10     {
11         sum=sum+N;
12        N--;
13     }
14     cout<<sum;
15}
16
queries leading to this page
get sum of digit of int c 2b 2bsum of digit in cppis there a limit when caculate total in c 2b 2bsum of digit in c 2b 2bint sum 7b 7d 3b in c 2b 2bsum of digits of numbers ic c 2b 2bc 2b 2b sum up numberssum of n numbers c 2b 2bhow to sum number c 2b 2bhow to add 10 in c 2b 2bsum from 0 to 100 in c 2b 2btotal of numbers c 2b 2bhow to get sum inputted in number in c 2b 2bhow to find the sum of possible sum of number in c 2b 2bsum of natural numbers in c 2b 2bthe sum of the digits of a number c 2b 2bc 2b 2b for loop to compute sumwrite a program to ask the user to enter 5 numbers then 2c display the sum of all integer numbers entered by the user c 2b 2b for calculate all numbersnatural numbers average c 2b 2bsum of n number c 2b 2bhow to find sum of digits in c 2b 2bn number sum using c 2b 2bfunction to find sum of digits in cppoutput sum in c 2b 2b programsum of number in c 2b 2b by taking inputssum the digit of number in cpphow to sum of 3 number in c 2b 2bsum of the digits number c 2b 2bhow to get a desired sum from a list of numbers in cppsum up all numbers in int array c 2b 2bsum of n natural numbers c 2b 2b write a c 2b 2b program to print sum of numbers from 2 to 8 c 2b 2b sum of digits of a numbersum 3d 2b10 in c 2b 2bc 2b 2b sum of all numbers up to a numberfind the sum from 1 to n c 2b 2bsum n number in c 2b 2balgorithm to find sum of n numbers in c 2b 2bwrite a program in c 2b 2b to find the sum of digitsc 2b 2b program to find sum of n numbers using for looptotal of numbers c 2b 2b programizhow to get sum of diits of number c 2b 2bfind the sum of digits of a number in c 2b 2bways to sum up to n c 2b 2bfind sum for numbers from 1 to 10 c 2b 2bsum of natural numbers in c 2b 2b codec 2b 2b program to find sum of n numbers using function overloadingsum of numbers fom 1 to n c 2b 2bsum of two numbers in c 2b 2bcreate a function to calculate the sum of any n numbers c 2b 2bsum of complex numbers in c 2b 2bsum numbers from 1to 100 c 2b 2bc 2b 2b program to find sum of n natural numbers using arrayc 2b 2b sum of digits in an integerfunction to sum all number in c 2b 2bsummation of two numbers in a loop c 2b 2bfor loop adding numbers c 2b 2bcalculate sum of numbers in array c 2b 2bcpp code for sum of natural numbersn number sum to find a element using c 2b 2breturn the sum of two numbers c 2b 2bc 2b 2b sumsum of all natural numbers to n cppsum of 3 digit number in c 2b 2bfind sum of digits of a number c 2b 2bcalculate sum of digits of a number c 2b 2bhow to find sum of natural number upto 10 5e9 in c 2b 2bwrite a program that calculates the sum for the following using a loop the user will provide the values for i and k c 2b 2bc 2b 2b problems to get the sum of digits in a numbersum of digitts of a number in cppsum of numbers from 1 to n c 2b 2b using for loopsum of natural numbers in cppfunction to give me sum of numbers in c 2b 2badd all the elements in numbers cpphwo to find sum of digits in c 2b 2bcheck for first n natural number in c 2b 2bsummation of c 2b 2bwrite a program to to add n sum of numbers using function overloading in c 2b 2bhow to find the sum of the digits in a number in c 2b 2bhow to get the sum of all digits in a number in c 2b 2bc 2b 2b calculate numbersc 2b 2b sum numberssum of numbers up to n c 2b 2bsum of two numbers c 2b 2bsum number c 2b 2bprint all the sum number from 1 to 15 in c 2b 2bwrite a program to to add sum of n numbers entered by user using function overloading in c 2b 2bsum of digits of a number in cppc 2b 2b code to find sum of digits of a numbersum of digits of number in c 2b 2bc 2b 2b program to find sum of natural numberssum of integers function c 2b 2bprogram to sum of digits of a number in c 2b 2bfind the sum of no c 2b 2bsum of all natural numbers c 2b 2bwhole numbers in c 2b 2bc 2b 2b the sum of a sequence of two numbers within a loop summationsum of n natural numbers in cppsum of digits of a number c 2b 2bsum of digits inputted c 2b 2bhow to sum all numbers under a given number c 2b 2bc 2b 2b sum up all numbersshort form of sum 3dsum 2bn in c 2b 2bc 2b 2b sum of numberscalculate 2 5e50 using c 2b 2bgenrate n natural number using generate in c 2b 2bc 2b 2b program to print the sum of n natural numberssum of odd numbers upto n in c 2b 2bc 2b 2b program to find sum of n natural numberssum of n natural numbers in c 2b 2bprogram to find sum of all digits of a number in c 2b 2bhow to find sum of digits of a number c 2b 2b in fastest wayhow to get the sum of all the number from 1 to n in c 2b 2b fastest waysum int c 2b 2bsum of 1 to n c 2b 2bc 2b 2b program to sum of digitshow natural number declare in c 2b 2bsum of digits in a number c 2b 2b functionsum digits of a number c 2b 2bget number that sums up to n c 2b 2bif 28sum 3c 2295 29 c 2b 2bc 2b 2b write a program that calculates the sum for the following using a loop sum of 3 numbers in cppa c 2b 2b for sum of digits in numbersum of number in c 2b 2bsum of numbers c 2b 2bhow to sum four numbers in a c 2b 2b functionsdetermine whether the sum is made using the numbers c 2b 2bfunction to get sum of the digits cpphow to find the sum of digits of an integer in c 2b 2bhow to compute 7e1 in c 2b 2bc 2b 2b sum of numbers loophow to print sum of numbs in c 2b 2bc 2b 2b code for sum of n numberssum of 1 to n numbers in c 2b 2bget the sum of digits of an int cppget sum of int c 2b 2bc 2b 2b program to find sum of even numbers in an arrayhow can you take input natural numbers in c 2b 2bsum of natural numbers in c 2b 2b in o 281 29print the sum of natural numbers c 2b 2bwrite a cpp program to calculate sum of first n natural numbersc 2b 2b sum the digits of an integerget sum of digits c 2b 2bdigit sum in c 2b 2bwhat is the sum of all numbers 1 to 1000 3f in c 2b 2bsum number from 1 to n in c 2b 2bhow to find the sum of digits in c 2b 2bsumm all numbers in array c 2b 2bsum c 2b 2bc 2b 2b sum of digitsfind sum of integers in number cppc 2b 2b program to find sum of digits of a given numberhow to sum of 4 digit numbers in c 2b 2bsum of numbers in c 2b 2bc 2b 2b program calculation of natural numbers from 1 to nhow to sum digits of a number in c 2b 2bc 2b 2b code different n numbers additionprogram to print sum of n numbers in a list in c 2b 2bsum of n numbers in cppwrite a program that calculates the summation for the following using a loop the user will provide the values for i and k c 2b 2bsum of digits of a number in c 2b 2bsum of natural numbers cppwrite a cpp program to calculate sum of first n natural numbers