Table of Contents
A palindrome number program in Java must use while and for loops to print all numbers that are palindromes, that is, those whose digits when read from left to right (top to bottom) are the same as when read from right to left (bottom to top). This article introduces a java program for palindrome that creates a palindrome number program. In this article, we will learn how to create a palindrome logic Number Program in Java using while and for loops. A palindrome number (number) will be one that remains the same after reversing its digits. For example, 1234 and 21 are palindromes whereas 7 and 8 are not because their order of digits changes when reversed. Most of the time we use String class methods to check if an input String is a palindrome or not but today we will learn how to do the same task using Java programming language and its built-in data types such as char, int, etc.
Palindrome Number Algorithm
A palindrome is a word, phrase, number or another sequence of units that can be read the same way in either direction. The simplest palindromes are words or phrases that are the same whether you read them left to right or right to left. You can also create palindromes using numbers. The number 1221 is a palindrome logic, as is the number 1001. There are many ways to create palindrome numbers using Java loops. In this blog post, we’ll show you how to create a palindrome number program in java program for palindrome using both the while and for loops. If you’re not sure which loop to use, try experimenting with both. We start by declaring an int variable called i at the top of our class. We then assign it the value 0 and initialize it on our constructor method using a for a loop. For demonstration purposes, we will have I represent different values from 1-9999 during each iteration of our loop:
Start with i=0; for(int i=0;i<10000;i++){ System.out.println(Palindrome: + String(i)); } After each iteration of the loop, we check if the current value of i matches the last digit before itself. If it does match (for example, if i is equal to 15), then all digits before the current one match up too (10). So we would add 10 to 15 and make the next iteration of our loop equal to 25 instead. When these two digits do not match up after 10 iterations, we need to make one more loop pass so that every digit has been checked at least once.
Grab the opportunity to learn Java with Entri! Click Here
Examples of Palindrome Number in Java
121, 393, 34043, 111, 555, 48084
Examples of Palindrome Number
LOL, MADAM
How to check whether the input number is Palindrome or not?
1: What is the default value of a boolean in Java?
A palindrome is a word, number, phrase, or another sequence of characters that reads the same backward as forward. Given an integer, check if that integer is a palindrome or not. For this problem do consider negative numbers also. An important thing to note here is that, if the input is a single digit then it’s always a palindrome.
There are many ways to solve this problem. We will discuss two methods here: using while loop and using for loop. The while loop method takes advantage of comparison with 0. The for loop method first converts number into string and reverses its value before checking whether it’s a palindrome logic or not. One more way to solve this problem is by doing a modulo operation on the number (number % 10). Note that, when performing modulo operation on 11, we get 1 and when we perform a modulo operation on 12 we get 0. Hence for every odd number (odd = 1 + 2 + 3 + 4+…) our modulo operation result will be 1 and for every even number (even = 2 + 4 + 6+…) our modulo operation result will be 0. Now use boolean variable ‘isPalindrome’ which tells us whether given input number is a palindrome or not.
Java program for palindrome with while loop is mentioned below;
package com.guru99; public class PalindromeNum { public static void main(String[] args) { int lastDigit,sum=0,a; int inputNumber=171; //It is the number to be checked for palindrome a=inputNumber; // Code to reverse a number while(a>0) { System.out.println("Input Number "+a); lastDigit=a%10; //getting remainder System.out.println("Last Digit "+lastDigit); System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); sum=(sum*10)+lastDigit; a=a/10; } // if given number equal to sum than number is palindrome otherwise not palindrome if(sum==inputNumber) System.out.println("Number is palindrome "); else System.out.println("Number is not palindrome"); } }
Code Output:
Input Number 171 Last Digit 1 Digit 1 was added to sum 0 Input Number 17 Last Digit 7 Digit 7 was added to sum 10 Input Number 1 Last Digit 1 Digit 1 was added to sum 170 Number is palindrome
Palindrome Program in Java using for loop
A palindrome is a word, phrase, number or another sequence of units that can be read the same way in either direction. The word palindrome was first used by the English writer Ben Jonson in 1624. The term is derived from the Greek words palindromos meaning running back or again. In programming, a palindrome is an integer that remains the same when its digits are reversed. For example, 121 is a palindrome because it’s equal to its reverse, 121. To check if an integer is a palindrome or not, we can use for loop to iterate over the number and check if the first digit is equal to the last digit and so on. To do this, we will create a function called isPalindrome which takes in an int as parameter and returns true if the number passed is a palindrome otherwise false. First, let’s import java.util.*; Then let’s create our function with one parameter called isPalindrome. If the given number cannot be divided by 2 then return false else proceed with remainder. For every iteration, check if current digit (n) equals previous digit (n-1). If both digits are equal then return true else return false
public class PalindromeNum { public static void main(String[] args) { int lastDigit,sum=0,a; int inputNumber=185; //It is the number to be checked for palindrome a=inputNumber; // Code to reverse a number for( ;a != 0; a /= 10 ) { System.out.println("Input Number "+a); lastDigit=a%10; //getting remainder System.out.println("Last Digit "+lastDigit); System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); sum=(sum*10)+lastDigit; a=a/10; } // if given number equal to sum than number is palindrome otherwise not palindrome if(sum==inputNumber) System.out.println("Number is palindrome "); else System.out.println("Number is not palindrome"); } }
Code Output:
Input Number 185 Last Digit 5 Digit 5 was added to sum 0 Input Number 1 Last Digit 1 Digit 1 was added to sum 50 Number is not palindrome
Learn to code from industry experts! Enroll here
Implementation of palindrome() Method
The isPalindrome() method is used to check whether a number is a palindrome or not. A palindrome number is a number that remains the same even when its digits are reversed. For example, 121 is a palindrome number but 112 is not. The method returns true if the number is a palindrome and false if it is not. If the digit at index i (starting from 1) is greater than 10, then we subtract 10 from this digit so that all of our digits fall between 0-9. We then compare the new digit with the old one (digit at index i-1). If they are equal then we have found a palindrome number! Else, the number is not a palindrome. Here’s how you can implement this program using a while loop:
The code starts by declaring an int variable called num which stores the inputted number. Next, we use the conditional operator ?: to test whether the string returned by System.out.println(num) is equal to . It’s just a shorthand way of writing if (System.out.println(num) == ”). Finally, we return either true or false depending on what was found during execution of the conditional statement above.
isPalindrome() – Determine whether a given number is palindromic
A palindromic number is a number that remains the same when its digits are reversed. For example, 121 is a palindromic number but 112 is not. The easiest way to check if a number is palindromic is to reverse it and compare it to the original number. If they are the same, then it’s a palindrome. You can write a program to check if a number is palindromic using either a while loop or a for loop. Let’s take a look at how to do this with each type of loop. Here’s an example of a while loop:
int n = 3;
while (n != 0) {
System.out.println(The number + n + is not a palindrome.);
n = n / 10;
Displaying output on the console
A palindrome is a word, phrase, number or another sequence of units that can be read the same way in either direction. A classic example is A man, a plan, a canal: Panama.
In programming, we often encounter situations where we need to check if a given string is a palindrome or not. This can be done using various looping constructs like while and for loop. In this blog post, we will see how to write a palindrome program in Java using both while and for loop.
The output of the program should be something like this: Input = racecar
Output = racecar
Input = Java2s
Output = Java2s
Input = to be or not to be
Learn Coding in your Language! Enroll Here!