Write a Java program and compute the sum of the digits of an integer

Program

import java.util.Scanner;
public class Demo { 
 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        long num = input.nextLong();
        int sum = 0;
        while (num != 0) {
            sum += num % 10;
            num /= 10;
        }
        System.out.println("The sum of the digits is: " + sum);
 
    }
 
    
}

Output:

Enter an integer: 1224
The sum of the digits is: 9

For latest job updates join Telegram Channel: https://t.me/sateeshm

Programs