# Print Sum of Digits

Write a function to print sum of the digits in a given integer n without converting it to String.

For example :

if n = 1234 the function should return 1+2+3+4 = 10

if n = 15 the function should return 1+5 =6

if n = 5 the function should return 5.

#### WannaBeCG Profile Answers by WannaBeCG

• Jul 23rd, 2010

import java.util.*;
{
public static void main(String a[])
{
int i = 12345, remainder, total = 0, temp = 0;Stack mystack = new Stack();

while(i != 0)
{
remainder = i % 10;
i = i / 10;
mystack.push(remainder);
}

while(!mystack.empty())
{
temp = (Integer)mystack.pop();
if(!mystack.empty())System.out.print(temp + " + ");
total += temp;
}

System.out.print(temp + " ");System.out.println(" = " + total);
}
}

#### TheOutlander Profile Answers by TheOutlander

• Jan 2nd, 2011

public static int GetSumOfDigits(int val)

{

return (val <= 0) ? 0 : ((val % 10) + GetSumOfDigits(val / 10));

}

#### Ali Abu tahoon

• Jul 20th, 2011

Code
1. public int getSumDig( long n)

2. {

3. int sum = 0;

4. while(n>0)

5. {

6. sum = sum + (int)(n % 10);

7. n = (int)(n / 10);

8. }

9. return sum;

10. }

Code
1. public int getSumDig( long n)

2.

3.         {

4.             int sum = 0;

5.             while(n>0)

6.             {

7.                 sum = sum + (int)(n % 10);

8.                 n = (int)(n / 10);

9.             }

10.           return sum;

11.         }

• Sep 22nd, 2011

Code
1. public class sumofdigit {

2.     public static void main(String args[])throws java.io.IOException {

3.         int n,x;

4.         int sum=0;

6.         while(n>0) {

7.             x=n%10;

8.             sum+=x;

9.         n=n/10;}

10.         System.out.println("The sum of digits is :"+n);

11.             }

12. }

13.

#### ashoknaina Profile Answers by ashoknaina

• Dec 19th, 2011

Code
1. main()

2. {

3. int num;

4. clrscr();

5. printf("enter a number u want to find sum:");

6. scanf("%d",&num);

7. sum(num);

8. getch();

9. }

10. void sum(int n)

11. {

12. int lastnum,firstnum,res=0;

13. while(n>0)

14.  {

15.   lastnum=n%10;

16.   res=res+lastnum;

17.   firstnum=n/10;

18.  }

19. printf("sum of the digits in a number:%d",res);

20. }

• Apr 17th, 2012

Code
1. #include<stdio.h>

2. #include<conio.h>

3. main()

4. {

5.         int r,i,n,s=0;

6.         printf("enter the value:=>");

7.         scanf("%d",&n);

8.         while(n>0)

9.         {

10.                 r=n%10;

11.                 s=s+r;

12.                 n=n/10;

13.

14.         }

15.         printf("the sum of diguts is %d

16. ",s);

17.         return 0;

18. }

#### deepak

• Jun 1st, 2012

Code
1. int main(int argc, char *argv[])

2. {

3. int i = 7685,k=0;

4. while(i)

5. {

6. k =k + i%10;

7. i = i/10;

8. }

9. printf("%d

10. ",k);

11. return 0;

12. }

#### brajeshupd Profile Answers by brajeshupd

• Jun 13th, 2012

Code
1.     #include<stdio.h>

2.     #define MIN_REMAINDER 0 /* For checking the number */

3.     int findSum(int);    /* recursive function to find the Sum */

4.     main()

5.     {

6.         int num = 1,sumOfDigit;

7.         printf("enter the value:=>");

8.         scanf("%d",&num);

9.         sumOfDigit = findSum(num);

10.         printf("

11. Sum of Digits of (%d) is : %d",num,sumOfDigit);

12.         return 0;

13.     }

14.

15.     int findSum(int num)

16.     {

17.         if(MIN_REMAINDER == num)

18.         {

19.             return 0;

20.         }

21.         return((num%10) + findSum(num/10));

22.     }

#### jenisha

• Jul 1st, 2012

Code
1. #include <stdio.h>

2.

3. main()

4. {

5.    int n, sum = 0, remainder;

6.

7.    printf("Enter an integer

8. ");

9.    scanf("%d",&n);

10.

11.    while(n != 0)

12.    {

13.       remainder = n % 10;

14.       sum = sum + remainder;

15.       n = n / 10;

16.    }

17.

18.    printf("Sum of digits of entered number = %d

19. ",sum);

20.

21.    return 0;

22. }

#### Sambuddha Chaudhuri

• Jul 16th, 2015

Code
1. import java.util.Scanner;

2.

3. public class fourdigit{

4.

5.     public static void main(String args[]){

6.

7.         int k = 0,j = 1,l = 1;

8.         Scanner sc = new Scanner(System.in);

9.         System.out.println("Enter the four digit number");

10.         int n = sc.nextInt();

11.         int temp = n;

12.         while(temp != 0){

13.             j = temp % 10;

14.             k = k + j;

15.             l = l *j;

16.             temp = temp /10;

17.         }

18.

19.         System.out.println("The sum of 4 digit number is :-"+k);

20.         System.out.println("The product of 4 digit number is :-"+l);

21.     }

22. }

#### Rkm

• Jan 23rd, 2016

recursive function in php

Code
1. function findSum(\$num) // recursive function in php

2. {

3.     if(\$num == 0) {

4.         return 0;

5.     }

6.

7.     return \$num%10 + findSum(floor(\$num/10));

8. }

9.

10. // print findSum(1235);

#### Shasan

• Mar 29th, 2016

Code
1.  public static int Sum()

2.         {

3.             int n = 1235678;

4.             int n1 = 0;

5.             int f = 0;

6.             while (n!= 0)

7.             {

8.                     n1 = n % 10;

9.

10.                     f = f + n1;

11.                     n = n / 10;

12.                    // return f;

13.

14.             }

15.

16.

17.             return f;

#### sravani reddy

• Sep 21st, 2017

Code
1. #include<stdio.h>

2. int main()

3. {

4. int rem,sum=0,n;

5. printf("enter the number:");

6. scanf("%d

7. ",&n);

8. while(n!=0)

9. {

10. rem=n%10;

11. sum=sum+rem;

12. n=n/10;

13. }

14. printf("the sum of digits of a number is %d",sum);

15. return 0;

16. }