GeekInterview.com
Series: Subject: Topic:
Question: 10 of 21

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.
Asked by: ayongying | Member Since May-2010 | Asked on: May 28th, 2010

View all questions by ayongying

Showing Answers 1 - 9 of 9 Answers
WannaBeCG

Answered On : Jul 23rd, 2010

View all answers by WannaBeCG

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);
}
}

  
Login to rate this answer.
TheOutlander

Answered On : Jan 2nd, 2011

View all answers by TheOutlander

public static int GetSumOfDigits(int val)

{

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

}

Yes  1 User has rated as useful.
  
Login to rate this answer.
Ali Abu tahoon

Answered On : 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.         }

Yes  1 User has rated as useful.
  
Login to rate this answer.

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.  

  
Login to rate this answer.
ashoknaina

Answered On : Dec 19th, 2011

View all answers by ashoknaina

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

  
Login to rate this answer.
mahamad

Answered On : 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. }

  
Login to rate this answer.
deepak

Answered On : 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. }

  
Login to rate this answer.
brajeshupd

Answered On : Jun 13th, 2012

View all answers by brajeshupd

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

  
Login to rate this answer.
jenisha

Answered On : 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. }

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Algorithm Questions

ColdFusion Interview Questions

Computer Awareness Questions

Delphi Interview Questions

Perl Interview Questions

PHP Interview Questions

Python Interview Questions

VBA Interview Questions

VoiceXML Interview Questions

XML Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.