Declare a variable of type int. Prompt the user for a positive three-digit number. Test to ensure the number is within the proper bounds. If the number is larger or less than three digits, output "Error" and terminate the program. Otherwise, calculate and print the sum of the three digits.

Respuesta :

Answer:

// Scanner class is imported to allow program

// receive input

import java.util.Scanner;

// Solution class is defined

public class Solution {

   // main method that signify beginning of program execution

   public static void main(String args[]) {

       // Scanner object scan is created

       // it receive input via keyboard

       Scanner scan = new Scanner(System.in);

       // Prompt is display asking the user to enter number

       System.out.println("Enter your three digit number: ");

       // the user input is stored at userInput

       int userInput = scan.nextInt();

       // if-statement that check if user input is three digit

       if(userInput < 100 || userInput > 999){

           // if input is three digit; it display error

           System.out.println("Error");

       } else{

           // the first digit is gotten as the quotient

           // when the input is divided by 100

           int firstDigit = userInput / 100;

           // the second digit is gotten by dividing the

           // result of the input modulus 100 by 10

           int secondDigit = (userInput % 100) / 10;

           // the third digit is gotten through

           // userInput modulus 10

           int thirdDigit = userInput % 10;

           // the addition of the three digit is assigned to sum

           int sum = firstDigit + secondDigit + thirdDigit;

           // the sum is displayed to the user

           System.out.println("The sum of the three digit is: " + sum);

       }

   }

}

Explanation:

The program is well commented and detailed. Images showing output of the program is attached.

Ver imagen ibnahmadbello
Ver imagen ibnahmadbello
Ver imagen ibnahmadbello
Ver imagen ibnahmadbello

Answer:

//import the Scanner class to allow for user's inputs

import java.util.Scanner;

//Declare a class for the application

public class ThreeDigit {

   //Declare a main method where execution begins

   public static void main(String[] args) {

       //Create an object of the Scanner class to allow user's inputs

       Scanner input = new Scanner(System.in);

       

       //Prompt the user to enter a 3-digit number

       System.out.println("Enter a positive three-digit number");

       

       //Receive the number with the scanner object created

       //And store it in an int variable

       int num = input.nextInt();

       

       //Check if the number is within range.

       //A 3-digit number is between 99 and 1000

       //If it is within range

       if(num > 99 && num < 1000){

           

           //GET THE FIRST DIGIT

           //The first digit is the result of  

           //the integer division of the 3-digit number with 100

           int fdigit = num / 100;

           

           //GET THE SECOND DIGIT

           //The second digit is the result of  the integer division of the

           // remainder, when the 3-digit number is divided by 100,

           // with 10 .

           int sdigit = (num%100) / 10;

           

           //GET THE THIRD DIGIT

           //The third digit is the remainder when the 3-digit number is divided

           // by 10 .

           int tdigit = (num % 10);

           

           //Sum the three digits

           int sum = fdigit + sdigit + tdigit;

           

           

           //print out the result

           System.out.println("The sum of the three digits is : " + sum);

       

       }          //End of if statement  

       

       //If it is not within range

       else {

           //print out an error message

           System.out.println("Error");

       }           // End of else statement

       

       

   }           //End of main method

   

}       //End of class declaration

===========================================================

Sample Output 1:

>> Enter a positive three-digit number

345

>> The sum of the three digits is : 12

============================================================

Sample Output 2:

>> Enter a positive three-digit number

5678

>> Error

============================================================

Explanation:

The code has been written in Java. It contains comments explaining every segment of the code. Kindly go through the comments.

The actual lines of code are written in bold face to separate them from comments.

Sample outputs have also been provided to give a feel of what the program outputs.