Sum of Digits of a Number

E · easyPractice

Given a number N, find the sum of all its digits.

Input
1234
Output
10
While N>0: digit = N%10, sum+=digit, N=N//10.
javamay contain transcription errors
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
while(n != 0){
sum += n % 10;
n /= 10;
}
System.out.println(sum);
}
}
← Subset Sum Problem (DP)Sum of Elements in Array →
Report an issue with this question