Write a program to compute the sum of the digits of a given integer number
Answers
Answer:
Please become my follower so that I can solve all your doubts........
Explanation:
import java.util.*;
public class Sum_Of_Digits
{
public static void main()
{
Scanner in= new Scanner (System.in);
int d,sum=0;
System.out.println ("Enter a number whose sum of digits is to be calculated");
int num= in.nextInt();
do
{
d= num/10;
sum=sum+d;
num=num/10;
}
while(num!=0);
System.outprintln("The sum of the digits="+" "+sum);
}
}
Answer:
Explanation:
#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
If you wish you can modify the input variable (n) and without using an additional variable (t) but it isn't recommended.