Question

Have the user enter an integer nbetween 100 and 999.  If the integer does not satisfy the...

Have the user enter an integer nbetween 100 and 999.  If the integer does not satisfy the given criteria (not in the correct range), or if the entered value is not an integer (use method hasNextInt(). have the user reenter an integer

Create an integer kthat is the reverse of n.   NOTE: k must be an integer

Display k.

Repeat

The program will stop when the user enters 0 (create a sentinel).

Example of the programs output:

Please enter an integer between 100 and 999:

Enter 0 if you want to exit

9000

The integer is not in the correct range/type

Please enter an integer between 1 and 999:

Enter 0 if you want to exit

20.5

The integer is not in the correct range/type

Please enter an integer between 100 and 999:

Enter 0 if you want to exit

670

The reverse of n = 678, is 876

Please enter an integer between 100 and 999:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

import java.io.*;
import java.util.*;
public class Test{
   public static void main(String args[]){
       int num;
       Scanner sc = new Scanner(System.in);
       System.out.println("Please enter an integer between 100 and 999:");
       System.out.println("Enter 0 if you want to exit");
       while(sc.hasNext()){
           if(sc.hasNextInt()){
               num = sc.nextInt(10);
               if(num == 0 )
                   break;
               if(num >= 100 && num <= 999){
                   System.out.println("The reverse of n = " + num + ", is " + reverse(num));

               }else{
                   System.out.println("The integer is not in the correct range/type");
               }
           }else{
               System.out.println("The integer is not in the correct range/type");
               sc.next();
           }
           System.out.println("Please enter an integer between 100 and 999:");
           System.out.println("Enter 0 if you want to exit");
       }
       sc.close();
   }
   public static int reverse(int num){
       int new_num = 0, remainder;
       while(num != 0){
           remainder = num % 10;
           new_num = new_num * 10 + remainder;
           num = num / 10;
       }
       return new_num;
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
Have the user enter an integer nbetween 100 and 999.  If the integer does not satisfy the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • I need help with the following assignment: Write an application named DailyTemps that continuously prompts a...

    I need help with the following assignment: Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from -20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message: Valid temperatures range from -20 to 130. Please reenter temperature. Before the program ends, display the...

  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • Design a Java program that asks the user to enter an integer number n and then...

    Design a Java program that asks the user to enter an integer number n and then generates an array of that many random points (x, y) with x- and y-coordinates in the range between 0 and 100. After this the program must ask the user to enter two diagonal points (x_1, y_1) and (x_2, y_2) of a rectangle with sides parallel to the coordinate axis (see the image below, where possible pairs of diagonal points are shown in red color)....

  • Write a program named ClassifyScores that classifies a series of scores entered by the user into...

    Write a program named ClassifyScores that classifies a series of scores entered by the user into ranges (0-9, 10-19, and so forth). The scores will be numbers between 0 and 100. Design and implement a program to prompt the user to enter each score separately. If the score is within the correct range, then increment the appropriate range If the score entered is greater than 100, display an error message, ignore the incorrect score, and prompt for the user to...

  • this is for java programming Please use comments Use the Account class created in homework 8_2...

    this is for java programming Please use comments Use the Account class created in homework 8_2 to simulate an ATM machine. Create ten checking accounts in an array with id 0, 1, …, 9, initial balance of $100, and annualInterestRate of 0. The system prompts the user to enter an id between 0 and 9, or 999 to exit the program. If the id is invalid (not an integer between 0 and 9), ask the user to enter a valid...

  • the user should be prompted to enter an integer and then your program will produce another...

    the user should be prompted to enter an integer and then your program will produce another integer depending on whether the input was even or odd. The following is an example of what you might see when you run the program; the input you type is shown in green (press Enter at the end of a line), and the output generated by the program is shown in black text. Enter an integer: 1234 Number is even, taking every other digit...

  • Using java Sample Outputs Write a program which asks the user for an integer and then...

    Using java Sample Outputs Write a program which asks the user for an integer and then prints the following patterns based on that integer. Input Validation For Pattern 1, the input must be a value between 1 and 999 For Pattern 2, the input must be a value between 1 and 26. Requirements: For Pattern 1: must be able to work with up to three digit numbers. You will want to use printf to get the spacing correct. For Pattern...

  • Write a program that reads an integer k from user and finds the number of elements...

    Write a program that reads an integer k from user and finds the number of elements that are divisible by k in the file assignment4.txt. A number n is divisible by k if n = kx for some integer x > 0. You can use the mod operator % to test for divisibility. The file assign4.txt has integer values in the range [0,100 ]. You should use end-of-file controlled loop for this problem. Sample execution is given below. Assume the...

  • write in python idle Write full program that asks the user to enter his/her name then...

    write in python idle Write full program that asks the user to enter his/her name then repeatedly ask to enter the temperature in Fahrenheit and convert it to Celsius, the program should then prompt the user if he/she wants to continue or exit the program. The formula for conversion is: °C = (°F - 32) x 5/9 The program should use a function for the conversion. An Example of a sample run should appear on the screen like the text...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT