Program to Find Sum of Numbers in C++
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.
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.
Algorithm of Program to Find the Sum of Numbers Using Loop
- Declare variables.
- Initialize sum with 0.
- Taking the number of inputs from the user.
- Getting the n number of inputs as specified in the previous step.
- Add each number to the sum of all the previous numbers to find the final sum.
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.
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.