Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java Program that prompts the user and obtains an integer number in a specified range (for this problem, range is given to be

Write a Java Program that prompts the user and obtains an integer number in a specified range (for this problem, range is given to be 0 to 999. If the number is not in the range (that is <0 or>999), an error is printed and program quits.

When the valid data is given, individual digits are then stripped and added together to find the sum of all digits. Output should reflect the values of individual digits and sum of all digits.

Palindrome Number: Input data would consist of at most 3 digits in the given range. If the digit in units position is same as the digit in 100s position, the number is said to be a palindrome. Examples of palindrome numbers: 0, 151, 282, 999, etc. Numbers that are not considered palindrome: 99, 123, 8, ..etc.

Your output should indicate whether the input number is a palindrome or not.

Algorithm:

Prompts the user to enter an integer in the range of 0 to 999.

Read the integer using a Scanner object (such as stdin or input or scnr as defined in your program), and save the value in to an int type variable (such as num)

Use the branch statement (if (condition) { ..} ) to check to see if the number is out of range, and if so, print an error statement (such as The number xxx is not in the range, where xxx is value of variable num) and quit the program (use: System.exit(0); )

Save the value of the num in another variable (such as : savedNum)

Strip individual digits and save them respectively in variables such as: d1, d10, d100 (corresponding to digit in 1s position, digit in 10s position, and digit in 100s position). For example, if an integer is 945, the d100 = 9, d10=4, and d1=5. The sum of all these digits is 18. Since d1 and d100 are not identical, the number is not a palindrome. Your program should output text that looks like this:

Expected output:

Input number is 945. It has 9 hundreds, 4 tens and a 5.

Sum of all the individual digits of 945 is 18

The number 945 is not a palindrome number.

Another run:

Input number is 949. It has 9 hundreds, 4 tens and a 9.

Sum of all the individual digits of 949 is 22

The number 949 is a palindrome number.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

Have roles been defined and assigned?

Answered: 1 week ago