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.

Questions by ayongying

Showing Answers 1 - 9 of 9 Answers

WannaBeCG

  • Jul 23rd, 2010
 

import java.util.*; 
public class AddInteger
{
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);
}
}

  Was this answer useful?  Yes

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

Code
  1. public class sumofdigit {

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

  3.         int n,x;

  4.         int sum=0;

  5.         n=(int) System.in.read();

  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.  

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

mahamad

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions