Sum Of Digits | Practice | GeeksforGeeks

Given a number, N. Find the sum of all the digits of N
 
Example 1:
Input:
N = 12
Output:
3
Explanation:
Sum of 12's digits:
1 + 2 = 3
Example 2:
Input:
N = 23
Output
5
Explanation:
Sum of 23's digits:
2 + 3 = 5


Your Task:
You don't need to read input or print anything. Your task is to complete the function sumOfDigits() which takes an integer N as input parameters and returns an integer, total sum of digits of N.

Expected Time Complexity: O(log10N)
Expected Space Complexity: O(1)
 
Constraints:
1<=N<=105
0
mayank180919992 weeks ago
 int sumOfDigits(int N){
        //code here
        int digit,sum=0;
        while(N!=0){
            digit=N%10;
            sum+=digit;
            N=N/10;
        }
    return sum;
    }
0
sayanmazumder9991 month ago
JAVA
        String s = String.valueOf(N); int sum = 0;       for(int i = 0; i < s.length(); i++) sum += (s.charAt(i) - '0');       return sum;
-1
0niharika22 months ago
if(N==0)           return 0;       return sumOfDigits(N/10) + N%10;
+2
0niharika22 months ago
int sum=0;       while(N){           sum+=N%10;           N/=10;       }       return sum;
0
iamprashantgehlot242 months ago
 int rem,sum=0;       while(N){           rem=N%10;           sum =sum+rem;           N=N/10;       }       return sum;
0
rupanjalborphukan2 months ago
Most efficient code:
Time taken for this code:  C++ : 0.0/1.1
                                         Java : 0.1/1.2
                                         Python : 0.0
 
int ans = 0;
while(N!=0){
	int digits = N%10;
	ans = ans+digits;
	N = N/10;
}
return ans;
0
amoghyelasangi2 months ago
class Solution:   def sumOfDigits (self, N):       sum = 0        for digit in str(N):           sum = sum+int(digit)       return sum
0
aadityajoshi2910203 months ago
Very Easy Solution :
class Solution{
    static int sumOfDigits(int N) {
        // code here
        int sum = 0;
        while(N != 0){
            int rem = N % 10;
            N = N/10;
            sum += rem;
        }
        return sum;
    }
}
0
sarangrajendrabadgujar3 months ago
JAVA Solution
class Solution{   static int sumOfDigits(int N) {       int i=0;       int count=0;       if(N<=99999){           i=N%10;           N=(N-i)/10;           count+=i;           i=N%10;           N=(N-i)/10;           count+=i;           i=N%10;           N=(N-i)/10;           count+=i;           i=N%10;           N=(N-i)/10;           count=count+i+N%10;       }       return count;   }}
+1
piyush9534974 months ago
JAVA
class Solution{   static int sumOfDigits(int N) {       if(N<=0){           return 0;       }       return N%10 + sumOfDigits(N/10);   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.