Write a program in java to input a number and calculate the sum of its digits
Answers
Answer:
✔Java Program to Compute the Sum of Digits in a given Integer
i) public class Digit_Sum.
ii) int m, n, sum = 0;
iii) Scanner s = new Scanner(System.
iv) System. out. print("Enter the number:");
v) m = s. nextInt();
vi) while(m > 0)
vii)n = m % 10;
viii)sum = sum + n;
I hope your ans is correct plz mark as brainlist dear like my ans
Answer:
import java.util.*:
public class Sum
{
public static void main(String args[])
{
Scanner in= new Scanner (System.in);
int a,n,m,l,s=0;
System.out.println("Enter the number");
a= in.nextInt();
n=Math.count(a);
for(m=1;m<=n;m++)
{
l=(a%10);
s=s+l;
System.out.println("The sum is =" +s);
}
}
}
Explanation:
As it is a terminating program so we should use loop here. Such that "for loop".
We cannot decide the number of digits of the number so we have to count the number of digits and terminate the loop that much of times.
To get the digits particularly we should devide the number by 10 and each time accept the remainder.