Q:

Write a Java program that reads an integer between 0 and 1000 and adds all the digits in the integer

-1

Write a Java program that reads an integer between 0 and 1000 and adds all the digits in the integer

Test Data
Input an integer between 0 and 1000: 565
Expected Output :
The sum of all digits in 565 is 16

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
public class Main {

    public static void main(String[] Strings) {

        Scanner input = new Scanner(System.in);

        System.out.print("Input an integer between 0 and 1000: ");
        int num = input.nextInt();

        int firstDigit = num % 10;
        int remainingNumber = num / 10;
        int SecondDigit = remainingNumber % 10;
        remainingNumber = remainingNumber / 10;
        int thirdDigit = remainingNumber % 10;
        remainingNumber = remainingNumber / 10;
        int fourthDigit = remainingNumber % 10;
        int sum = thirdDigit + SecondDigit + firstDigit + fourthDigit;
        System.out.println("The sum of all digits in " + num + " is " + sum);

    }
}

Sample Output:

Input an integer between 0 and 1000: 565                                                                      
The sum of all digits in 565 is 16 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now