Program to Find Sum of Numbers in C++

Video Tutorial
FREE
Sum of Numbers thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

We will solve a program in C++ to find the sum of two numbers, the sum of n numbers, and then the sum of the digits of a number. We will solve this using a loop and mathematical operations only.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Sum of 2 Numbers in C++

Program to add 2 numbers.

Example Program

Output

In the above program, we have taken two integers a and b, and then we have calculated their sum using the addition operator and stored the sum value inside a variable sum. Finally, we are printing the value of sum variable.

Sum of N Numbers in C++

This is similar to the previous program. But instead we have N numbers, so we need to have a loop, to take the N numbers in put as we don't already know N. We first discuss the algorithm and then look at the program to find the sum of N numbers.

Sharpen Your Fundamentals with Free Learning

Algorithm of Program to Find the Sum of Numbers Using Loop

  1. Declare variables.
  2. Initialize sum with 0.
  3. Taking the number of inputs from the user.
  4. Getting the n number of inputs as specified in the previous step.
  5. Add each number to the sum of all the previous numbers to find the final sum.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Program for the Sum of n Numbers Entered by the User

Example Program

Output

In the above code, we are adding every number entered by the using loop. The above program will run the number of times the user has entered the value of n.

Sum of Digits of an integer in C++

Now, we will look at a program in C++ to calculate the sum of an integer's digits. For example, if the integer is 23145, the sum of its digits is 2 + 3 + 1 + 4 + 5 = 15.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Sum of Digits Algorithm

The steps to solve the program to find the sum of digits in C++.

Step 1: User input

In this step, we get the input from the user.

Step 2: Modulus/remainder of user input

We have to find the modulus/remainder of the user input.

Step 3: Sum of modulus

In this step, we have to sum the remainder of the number we found in the previous step.

Step 4: Dividing the result of step 3 by 10.

We have to divide the number by 10 of the result that we got in our previous step.

Step 5: Repeatition of step 2 till number is greater than 0.

Repeat from step two till the number is greater than 0.

Program for the Sum of the digits of a number in C++

Example

OUTPUT

In the above program, suppose we have the integer 23145. We are taking the digits one by one from right. So, if we perform the modulo operation, 23145 % 10 = 5, we get 5. So we add the value 5 to the sum variable. Now, since we have taken the contribution of 5 we have to remove it from the number. We can do this by dividing the integer 23145 by 10, resulting in 2314, and then repeat the algorithm. Here, one thing to note is that 23145, when divided by 10, results in 2314 instead of 2314.5 because when two integers are divided in C++, the remainder is ignored.

Conclusion

  • There is a difference between finding the sum of numbers and the sum of digits.
  • Sum of digits means finding the sum of the digits of the number entered.
  • Sum of numbers means finding the sum of different numbers of any length using loops.
  • Sum of numbers can be found using loops and mathematical operations.