Sum of digits of a Number in C++
Program to Find the Sum of Digits of a Number in C++
Here we will discuss how to find the sum of digits of a number in C++ programming language.
We will use loops along with two arithmetic operators: 1. Modulo Operator : '%' 2. Division Operator: '/' Eg: number = 12345 Sum of digit of number = 1 + 2 + 3 + 4 + 5 Sum = 15
Algorithm:-
For user input num
- Initialize sum = 0
- While extracting the last digit of num add it to the sum (sum += num % 10)
- Reduce the number (num = num / 10
- Keep doing this until num becomes 0
C++ Code
//C++ Program //Sum of digits in a number #include<iostream> using namespace std; int main () { int num, sum = 0; num=1234; cout <<"\nThe number is: " << num; //loop to find sum of digits while(num!=0){ sum += num % 10; num = num / 10; } //output cout <<"\nSum of digits: " << sum; return 0; } // Time complexity : O(N) // Space complexity : O(1) // where N is number of digits in num
Output
The number is:1234 Sum of digits : 10
Method 2
This method uses recursion in c++. Please go through this page if you are new to recursion
C++ Code
//C++ Program sum of digits in a number using recursion #include<iostream> using namespace std; int getSum(int num, int sum){ if(num == 0) return sum; sum += num % 10; return getSum(num/10, sum); } int main () { int num, sum = 0; num=12345; cout <<"\nThe number is:"<<num; cout <<"\nSum of digits : " << getSum(num, sum); return 0; } // Time complexity : O(N) // Space complexity : O(1) // Auxiliary Space complexity : O(N) due to function call stack // where N is number of digits in num
Output
The number is:12345 Sum of digits : 15
Method 3
This method uses recursion in c++. Please go through this page if you are new to recursion.
This method has a little improvement over the previous recursive method as it doesn’t use extra sum parameter in the recursive function.
C++ Code
//C++ Program sum of digits in a number using recursion #include<iostream> using namespace std; int getSum(int num){ if(num == 0) return 0; return (num % 10) + getSum(num/10); } int main () { int num; num=12345; cout <<"\nThe number is:"<<num; cout <<"\nSum of digits : " << getSum(num); return 0; } // Time complexity : O(N) // Space complexity : O(1) // Auxiliary Space complexity : O(N) due to function call stack // where N is number of digits in num
Output
The number is:12345 Sum of digits : 15
Method 4
Make sure to check ASCII Table here for below method
This is an Alert
There may be cases when the number is very very large like - 9876543219876543219876
Such numbers can not be even handled by long long type of datatype since it only supports 1019 length max approx.
In this case we will use a string to store the number and below program will handle string data and find sum using ASCII work around
Such numbers can not be even handled by long long type of datatype since it only supports 1019 length max approx.
In this case we will use a string to store the number and below program will handle string data and find sum using ASCII work around
Code
// Time Complexity : O(N) // Space Complexity : O(1) #include<iostream> using namespace std; int getSum(string num) { int sum = 0; // Traverse through the whole string(char array) for (int i = 0; i < num.length(); i++) { // Ascii value pf numbers start from 48 // subtracting 48 will give us value in int sum = sum + num[i] - 48; } return sum; } int main() { string num = "9876543219876543219876"; // can also use below // int len = sizeof(num)/sizeof(num[0]); cout<< "Sum: " << getSum(num); return 0; }
Output
Sum: 120
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment