Q:

Java Program to add all the digits of a number between 0 and 1000

0

Program to add all the digits of a number between 0 and 1000 by getting input from the user.

All Answers

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

import java.util.Scanner;
public class demo1 {

    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 remainder = num / 10;
        int SecondDigit = remainder % 10;
        remainder = remainder / 10;
        int thirdDigit = remainder % 10;

        int sum = thirdDigit + SecondDigit + firstDigit;

        System.out.println("The sum of all digits in " + num + " is " + sum);

    }
}

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