# 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

```Codepublic int getSumDig( long n)
{
int sum = 0;
while(n>0)
{
sum = sum + (int)(n % 10);
n = (int)(n / 10);
}
return sum;
}```

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.         }

#### Kunal Priyadarshi Profile Answers by Kunal Priyadarshi

• Sep 22nd, 2011

```Codepublic class sumofdigit {
public static void main(String args[])throws java.io.IOException {
int n,x;
int sum=0;
while(n>0) {
x=n%10;
sum+=x;
n=n/10;}
System.out.println("The sum of digits is :"+n);
}
}
```

#### ashoknaina Profile Answers by ashoknaina

• Dec 19th, 2011

```Codemain()
{
int num;
clrscr();
printf("enter a number u want to find sum:");
scanf("%d",&num);
sum(num);
getch();
}
void sum(int n)
{
int lastnum,firstnum,res=0;
while(n>0)
{
lastnum=n%10;
res=res+lastnum;
firstnum=n/10;
}
printf("sum of the digits in a number:%d",res);
}```

• Apr 17th, 2012

```Code#include<stdio.h>
#include<conio.h>
main()
{
int r,i,n,s=0;
printf("enter the value:=>");
scanf("%d",&n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;

}
printf("the sum of diguts is %d
",s);
return 0;
} ```

#### deepak

• Jun 1st, 2012

```Codeint main(int argc, char *argv[])
{
int i = 7685,k=0;
while(i)
{
k =k + i%10;
i = i/10;
}
printf("%d
",k);
return 0;
}```

#### brajeshupd Profile Answers by brajeshupd

• Jun 13th, 2012

```Code    #include<stdio.h>
#define MIN_REMAINDER 0 /* For checking the number */
int findSum(int);    /* recursive function to find the Sum */
main()
{
int num = 1,sumOfDigit;
printf("enter the value:=>");
scanf("%d",&num);
sumOfDigit = findSum(num);
printf("
Sum of Digits of (%d) is : %d",num,sumOfDigit);
return 0;
}

int findSum(int num)
{
if(MIN_REMAINDER == num)
{
return 0;
}
return((num%10) + findSum(num/10));
}```

#### jenisha

• Jul 1st, 2012

```Code#include <stdio.h>

main()
{
int n, sum = 0, remainder;

printf("Enter an integer
");
scanf("%d",&n);

while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}

printf("Sum of digits of entered number = %d
",sum);

return 0;
}```

#### Sambuddha Chaudhuri

• Jul 16th, 2015

```Codeimport java.util.Scanner;

public class fourdigit{

public static void main(String args[]){

int k = 0,j = 1,l = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the four digit number");
int n = sc.nextInt();
int temp = n;
while(temp != 0){
j = temp % 10;
k = k + j;
l = l *j;
temp = temp /10;
}

System.out.println("The sum of 4 digit number is :-"+k);
System.out.println("The product of 4 digit number is :-"+l);
}
}```

#### Rkm

• Jan 23rd, 2016

recursive function in php

```Codefunction findSum(\$num) // recursive function in php
{
if(\$num == 0) {
return 0;
}

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

// print findSum(1235);```

#### Shasan

• Mar 29th, 2016

```Code public static int Sum()
{
int n = 1235678;
int n1 = 0;
int f = 0;
while (n!= 0)
{
n1 = n % 10;

f = f + n1;
n = n / 10;
// return f;

}

return f;```

#### sravani reddy

• Sep 21st, 2017

```Code#include<stdio.h>
int main()
{
int rem,sum=0,n;
printf("enter the number:");
scanf("%d
",&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("the sum of digits of a number is %d",sum);
return 0;
}```  