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 - 13 of 13 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

ashoknaina

  • Dec 19th, 2011
 

Code
  1. span style="color: #ff0000;">"enter a number u want to find sum:");

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

  3. }

  Was this answer useful?  Yes

mahamad

  • Apr 17th, 2012
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. "enter the value:=>");

  4.         scanf("%d""the sum of diguts is %d

  5. "

  Was this answer useful?  Yes

deepak

  • Jun 1st, 2012
 

Code
  1. span style="color: #ff0000;">"%d

  2. "

  Was this answer useful?  Yes

brajeshupd

  • Jun 13th, 2012
 

Code
  1.     #include<stdio.h>

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

  3. /* recursive function to find the Sum */"enter the value:=>");

  4.         scanf("%d""

  5. Sum of Digits of (%d) is : %d"

  Was this answer useful?  Yes

jenisha

  • Jul 1st, 2012
 

Code
  1. #include <stdio.h>

  2. "Enter an integer

  3. ");

  4.    scanf("%d""Sum of digits of entered number = %d

  5. "

  Was this answer useful?  Yes

Sambuddha Chaudhuri

  • Jul 16th, 2015
 

Code
  1. span style="color: #006699;">java.util.Scanner"Enter the four digit number""The sum of 4 digit number is :-""The product of 4 digit number is :-"+l);

  2.     }

  3. }

  Was this answer useful?  Yes

Rkm

  • Jan 23rd, 2016
 

recursive function in php

Code
  1. span style="font-style: italic;">// recursive function in php

  2. // print findSum(1235);

  Was this answer useful?  Yes

Shasan

  • Mar 29th, 2016
 

Code
  1. span style="font-style: italic;">// return f;

  Was this answer useful?  Yes

sravani reddy

  • Sep 21st, 2017
 

Code
  1. #include<stdio.h>

  2. "enter the number:");

  3. scanf("%d

  4. ""the sum of digits of a number is %d"

  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.

 

Related Answered Questions

 

Related Open Questions