Enter any integer number as input. After that, we use modulus and division operation respectively to find the sum of digits of the number as output.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
int no,no1,sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number tofind the sum of digits : ");
no = sc.nextInt();
while(no>0){
no1=no%10;
sum=sum+no1;
no=no/10;
}
System.out.println("Sum of all digits of a number : "+sum);
}
}
Comments
Post a Comment