Sum of the digits
Problem to sum the digits in given numbers.
Example-1:
Input : n = 12345 Output : 15 Explain : 1+2+3+4+5 = 15
Example-2:
Input : n = 1541 Output : 11 Explain : 1+5+4+1 = 11
Solution
public class Main { public static void main(String [] args) { int n = 12345; int sum = 0; while(n!=0) { sum = sum + n % 10; n = n/10; } System.out.print(sum); } }
n = 12345 sum = 0 while(n!=0): sum += n % 10 n = n//10 print(sum)
Output
15