write a program that prints the palindromic numbers (in decimal ) between 0-9999 into the screen.
Note :it should be written in java language.
Answer:
Here is the code, the explanation is in the comments. Please go through them for better understanding of the code. All you need to do is to create a class called PalindromeRange.java, paste the below code and execute. Please do not hesitate to leave a comment if you don't understand the code. Good Luck!!!
PalindromeRange.java
public class PalindromeRange { // Start of class
int isPalindrome(int number) { // check whether the number is palindrome
int originalNumber = number; // preserve the original number
passed into the method for later comparison
int reverse = 0; // set reverse to 0 to start with
while(number != 0) { // until the number is not 0, loop in and
keep dividing the number
int remainder = number % 10; // do the modulus division on the
number
reverse = reverse * 10 + remainder; // keep adding the
remainders
number /= 10; // perform regular division
}
if (originalNumber == reverse){ // if the original number and
reversed numbers are equal, then return 0
return 0;
}else {
return 1; // if original number and reversed numbers are not equal,
return 1
}
}
void range(int startRange, int endRange) // This is the method
where you display the palindrome numbers
{
for (int i = startRange; i <= endRange; i++) { // Start a for
loop 0-9999
if (isPalindrome(i) == 0){ // call the isPalindrome() in the for
loop
System.out.print(i + " "); // Display the palindrome numbers
}
}
}
public static void main(String [] args) { // Start of main()
PalindromeRange palindromeRange = new PalindromeRange(); //
Create an obj of PalindromeRange()
palindromeRange.range(0, 9999); // call the method range()
} // End of main()
} // End of class
O/P Screenshot for your reference
Get Answers For Free
Most questions answered within 1 hours.