lab 3 update

This commit is contained in:
HeshamTB 2019-02-17 17:06:36 +03:00
parent 1139b17d66
commit bb8ed4c47f
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package lab.four;
public class PalinTest {
public static void main(String[] args) {
Palindrome app = new Palindrome();
app.checkPalindrome();
}
}

View File

@ -0,0 +1,75 @@
package lab.four;
import java.util.Scanner;
public class Palindrome {
public void checkPalindrome()
{
Scanner input = new Scanner( System.in );
int number; // user input number
int digit1; // first digit
int digit2; // second digit
int digit3;
int digit4; // fourth digit
int digit5; // fifth digit
int digits; // number of digits in input
number = 0;
digits = 0;
/* Write code that inputs a five-digit number. Display an error message
if the number is not five digits. Loop until a valid input is received. */
boolean isFive = false;
while (!isFive) {
System.out.print("Enter a 5-digit number: ");
number = input.nextInt();
if (number > 9999 && number < 100000) {
isFive = true;
}
else {
System.out.println("Number must be five digits");
}
}
/* Write code that separates the digits in the five digit number. Use
division to isolate the left-most digit in the number, use a remainder
calculation to remove that digit from the number. Then repeat this
process. */
digit5 = number % 10;
number = number / 10;
digit4 = number % 10;
number = number / 10;
digit3 = number % 10;
number = number / 10;
digit2 = number % 10;
number = number / 10;
digit1 = number % 10;
number = number / 10;
// System.out.println(digit1);
// System.out.println(digit2);
// System.out.println(digit3);
// System.out.println(digit4);
// System.out.println(digit5);
/* Write code that determines whether the first and last digits are
identical and the second and Fourth digits are identical. Output
whether or not the original string is a palindrome. */
if (digit1 == digit5) {
if (digit2 == digit4) {
System.out.println("The number is a palindrome");
}
}
System.out.println("The number is not a palindrome");
}
}