Sum of digits in C++
Sum of digits in C++
There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases that help us to find the sum of the digits of a number entered by the user. Let us now directly look at what are we talking about.
Example: 153=> 1+5+3= 9
There is a certain number of simple to complex algorithms. In some languages, there are predefined keywords to do such activities and to reduce the number of codes and focus more on other important aspects.
Let’s look at the different approaches for finding some of the digits of the number in C++.
Example 1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int value, number, sum = 0;
cout << "Enter any number : ";
cin >> value;
number = value;
while (number != 0)
{
sum = sum + number % 10;
number = number / 10;
}
cout << "The sum of the digits of "
<< value << " is " << sum;
}
Output:
Explanation:
In the above code, we have defined three variables "number", "value" & "sum". We are taking input from the user and iterating the condition till it is less than zero. Logically, to find the sum of digits of a number we need to find it’s modulo division by 10 which gives the remainder, and on further division by 10 gives the remaining single number. This process is repeated until the sum is calculated singularly using the digits. Later the "sum" variable stores the value and prints it onto the console.
Example 2:
#include<bits/stdc++.h>
using namespace std;
int DigitSum(int number)
{
if (number == 0)
return 0;
return (number % 10 + DigitSum(number / 10));
}
int main()
{
int number;
cin>>number;
int result = DigitSum(number);
cout << "Sum of digits in "<< number
<<" is "<<result << endl;
return 0;
}
Output:
Explanation:
Above code uses recursive approach to find the sum of digits of a number. Its algorithm can be explained as follows:
Recursive Algorithm:
Let us take a number 12345.
START
1) 54321 % 10 = 5 + ( proceed 54321/10 to next step )
2) 1234 % 10 = 4 + ( proceed 4321/10 to next step )
3) 123 % 10 = 3 + ( proceed 321/10 to next step )
4) 12 % 10 = 2 + ( proceed 21/10 to next step )
5) 1 % 10 = 1 + ( proceed 1/10 to next step )
6) Step 6-> 0
STOP
In the above code recursive algorithm breaks-up the entered number individually into single units and the function we used returns them by the summation of modulo division and division by 10.
There are yet different approaches that involve object-oriented programming object and classes concept. Although these are time-consuming, it can be used when we are scaling up our algorithms into a proper sequence.
Reverse of a number in C++
Reversing any number means to rotate the original number upside down or say backward. Suppose, the reverse of 54321 is 12345.
To reverse any number in C++ or any other programming language, we are required to know loops. Without the understanding of loops, we won't be able to understand it.
Let us look how to reverse a number using different approaches.
Example 1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int originalNumber, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> originalNumber;
while(originalNumber != 0)
{
remainder = originalNumber%10;
reversedNumber = reversedNumber*10 + remainder;
originalNumber /= 10;
}
cout << "Reversed Number = " << reversedNumber;
return 0;
}
Output:
Explanation:
The above code takes an input from the user through the variable "originalNumber". Then the while loop iterates till n!=0.
In each iteration, the value of the entered number is divided by 10 and the reversedNumber is calculated. The number n is then decreased by 10 folds and later the remainder is added to find the reverse of the number.
Algorithmic Logic:
Let us see the process in details.
- Iteration starts from n=567
- remainder( 567 % 10) = 7
- reversedNumber = (0*10+7)=7
- Second iteration starts from n=56
- Remainder(56%10) = 6
- reversedNumber =(0*10=6)=6
- Repeat the process till n==0
- STOP
Finally, the variable 'reversedNumber' is assigned the number which is then printed on the console.
Since we are now very clear behind the scenes, we can proceed to see other examples for finding the reverse of a number.
Example 2:
#include <bits/stdc++.h>
using namespace std;
int DigitReverse(int number)
{
int rev_num = 0;
while(number > 0)
{
rev_num = rev_num*10 + number%10;
number = number/10;
}
return rev_num;
}
int main()
{
int number;
cin>>number;
cout << "Reverse of number is "
<< DigitReverse(number);
return 0;
}
Output:
Explanation:
In the above code, we have defined a function named 'DigitReverse' in which the number entered by the user will be treated as an argument. The function loops till the number equal to zero and the remainder are later added to the modulo division of the number entered. The resultant number is the reverse of the number which is printed on the console.