Results 1 to 1 of 1
Thread: Write a java program
- 05-16-2013, 09:23 PM #1Member
- Join Date
- May 2013
- Rep Power
Write a java program
I am completely new to computer science and am having a hard time writing the program.
A palindrome is a number that is the same backwards and forwards. Write a Java Program without using the “if statements” strings or loops that asks the user to enter a number and then tells him whether it is a palindrome or not.
1) Assume The number 10 is not a palindrome
2) You can extract a digit from a number by :
1) to extact the ones digit of a number, you can take a number modulus 10
2) Whenenever you divide a number by 10, you shift the decimal points of the number
Remember that a && b is true if both a and b are true
Remember that a||b is true as long as at least one of a and b are true
A general outline for solving this problem is given:
1. Store into 4 different int variables each of the 5 digits of the number.
2. Create a boolean variable isOneDigitPalindrome and come up with an expression to check if the number is a one digit palindromic number. There may be several correct expressions here: For example, a number is a one digit palindrome if and only if the 10s, 100s, and 1000s digits all are 0. An alternate check would be whether the number is less than 10.
3. Repeat the previous step for 2,3,and 4 digits.
4. A number is a palindrome if it is a one digit palindrome OR a two digit palindrome OR a three digit palindrome OR a four digit palindrome.
So far I know that
To test for palindrome, since you know the max is 4 digits, you only have 4 cases to test for:
1) 4 digit number
---- no leading zeroes
---- first digit == last digit
---- second digit == third digit
2) 3 digit number
---- 1 leading zero
---- second digit == last digit
---- third digit doesn't matter
3) 2 digit number
---- 2 leading zeroes
---- third digit == last digit
4) 1 digit number (always a palindrome)
---- 3 leading zeroes
---- lone digit, so it doesn't matter
The first digit is:
digit1 = n % 10;
The second digit is:
digit2 = (n - digit1) % 100;
The third digit is:
digit3 = (n - digit1 - 10 * digit2) % 1000;
I have started my program. I do not know how to continue after the last step. Any hint or at least a few steps might help me started.
public class PalindromeChecker
public static boolean specPalindrome(int n)
Scanner reader = new Scanner(System.in);
System.out.println("Enter a postive number that is 4 or fewer digits");
int firstDigit = reader.nextInt();
- By manideep.d132 in forum New To JavaReplies: 7Last Post: 11-30-2011, 06:58 PM
- By yungcotter in forum New To JavaReplies: 7Last Post: 12-13-2010, 01:35 PM
- By priyamurugar in forum NetworkingReplies: 1Last Post: 04-27-2010, 11:20 AM
- By maneibr in forum New To JavaReplies: 1Last Post: 03-13-2008, 01:28 PM