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:

Here is my code below. can you show what else needs to be done to achieve the above ?

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.

MY CODE

import java.util.Scanner;

public class PA2 {

public static void main(String args[]) {

int num, savedNum, sum = 0;

Scanner scnr = new Scanner(System.in);

System.out.println("Enter an integer in the range of 0 to 999:");

num = scnr.nextInt();

savedNum = num;

// Getting hundreds by dividing number with 100 to get quotient

int hundreds = num / 100;

// Getting ones digit by using the percentile

int ones = num % 10;

// Storing the quotient of number by dividing with 10

num = num / 10;

int tens = num % 10;

System.out.println("Input number is " + savedNum);

System.out.println("It has " + hundreds + " hundreds, " + tens + " tens and a " + ones);

sum = hundreds + ones + tens;

System.out.println("Sum of all the individual digits of " + savedNum + " is " + sum);

if(hundreds == ones)

System.out.println("The number " + savedNum + " is a palindrome number.");

else

System.out.println("The number " + savedNum + " is not 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

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions