Write a CPP program to calculate sum of first N natural numbers

// Method 1: Mathematical -> Sum up numbers from 1 to n 
int sum(int n){
	return (n * (n+1)) / 2;
}

// Method 2: Using a for-loop -> Sum up numbers from 1 to n 
int sum(int n){
	int tempSum = 0; 
  	for (int i = n; i > 0; i--){
     	tempSum += i;  
    }
  	return tempSum; 
}

// Method 3: Using recursion -> Sum up numbers from 1 to n 
int sum(int n){
	return n > 0 ? n + sum(n-1) : 0; 
}

0
0
Awgiedawgie 440220 points

                                    
#include <iostream>

using namespace std;

int main(){
	int N,i,sum=0;
	cin>>N;
     for(i=0;i<=N;)
     {
         sum=sum+N;
        N--;
     }
     cout<<sum;
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
n number sum to find a element using c++ n number sum using c++ sum of 1 to n numbers in c++ sum of n natural numbers in cpp sum number from 1 to N IN C++ get the sum of digits of an int cpp get number that sums up to n c++ print the sum of natural numbers c++ c++ program to print the sum of n natural numbers sum of digits of numbers ic c++ HOW TO SUM OF 3 NUMBER IN C++ c++ program to sum of digits find sum for numbers from 1 to 10 c++ calculate sum of digits of a number c++ sum of n numbers c++ C++ program to find sum of n natural numbers using array calculate sum of numbers in array c++ c++ sum of digits in an integer sum of digits inputted c++ find the sum of digits of a number in c++ function to sum all number in c++ get sum of digit of int c++ get sum of int c++ how to print sum of numbs in c++ sum number c++ hwo to find sum of digits in c++ sum of n number c++ create a function to calculate the sum of any n numbers c++ sum of digit in cpp sum of 1 to n c++ find the sum from 1 to n c++ sum of natural numbers cpp determine whether the sum is made using the numbers c++ c++ problems to get the sum of digits in a number sum of natural numbers in c++ summ all numbers in array c++ sum n number in c++ sum of integers function c++ how to sum of 4 digit numbers in c++ digit sum in c++ how to find sum of digits in c++ sum digits of a number c++ return the sum of two numbers c++ sum of n natural numbers in c++ how to find the sum of digits of an integer in c++ get sum of digits c++ sum of digits of a number in c++ sum int c++ C++ sum the digits of an integer how to sum all numbers under a given number c++ sum of natural numbers in cpp how to sum number c++ sum of 3 digit number in c++ sum of digitts of a number in cpp how to find the sum of digits in c++ how to get the sum of all digits in a number in c++ c++ program to find sum of natural numbers add all the elements in numbers cpp how to find the sum of the digits in a number in c++ find sum of digits of a number c++ c++ program to find sum of digits of a given number function to get sum of the digits cpp c++ sum of digits of a number c++ sum of digits sum of digits of a number in cpp program to find sum of all digits of a number in c++ sum of digits of a number c++ sum of n numbers in cpp sum the digit of number in cpp sum up all numbers in int array c++ sum of the digits number c ++ the sum of the digits of a number c++ sum of all natural numbers to N cpp sum of all natural numbers c++ c++ code to find sum of digits of a number c++ program to find sum of n numbers using function overloading write a program to to add sum of n numbers entered by user using function overloading in c++ write a program to to add n sum of numbers using function overloading in c++ C++ program calculation of natural numbers from 1 to n whole numbers in c++ natural numbers average c++ sum of natural numbers in c++ code how can you take input natural numbers in c++ c++ code for sum of n numbers for loop adding numbers c++ how to get a desired sum from a list of numbers in cpp sum of number in c++ sum numbers from 1to 100 c++ sum from 0 to 100 in c++ What is the sum of all numbers 1 to 1000? in c++ how to get sum of diits of number c++ function to give me sum of numbers in c++ Write a C++ program to print sum of numbers from 2 to 8. short form of sum=sum +n in c++ c++ sum up all numbers C++ Write a program that calculates the sum for the following using a loop. c++ program to find sum of n numbers using for loop C++ the sum of a sequence of two numbers within a loop summation Write a program that calculates the summation for the following using a loop. The user will provide the values for i and k. C++ Write a program that calculates the sum for the following using a loop. The user will provide the values for i and k. C++ summation of two numbers in a loop C++ c++ for calculate all numbers c++ calculate numbers how natural number declare in c++ output sum in c++ program sum of numbers in c++ if (sum < 2295) c++ Write a program to ask the user to enter 5 numbers. Then, display the sum of all integer numbers entered by the user. sum of numbers c++ c++ code different n numbers addition print all the sum number from 1 to 15 in c++ sum =+10 in c++ c++ sum of numbers loop c++ sum how to add 10 in c++ summation of c++ sum c++ Calculate 2^50 using c++ sum of natural numbers in c++ in O(1) C++ for loop to compute sum c++ sum numbers check for first N natural number in C++ genrate n natural number using generate in c++ how to compute ~1 in c++ Write a CPP program to calculate sum of first N natural numbers find the sum of no c++ is there a limit when caculate total in c++ sum of n natural numbers c++ c++ sum up numbers c++ sum of numbers c++ sum of all numbers up to a number
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source