For Loop

# Java Palindrome Number Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

• Anonymous says:

How i can get a the code for this
1
11
121
1331
14641

int j=1;
for(int i=1;i<=5;i++){
System.out.println(j);
j=j*11;
}

• MasterWalpha says:

Those numbers in that order come from the pascal triangle.

• MasterWalpha says:

They are in a sequence of adding the two numbers above the…

• Mitau Mitau says:

how cn i show that a number is a palindrome for only counting the number of digits. eg for only 5 digit numbers?

• the above code is not correct. below is the correct to find the palindrome number:-

public static void main(String[] args) {

//array of numbers to be checked

int numbers[] = new int[]{121,13,34,11,22,54};

//iterate through the numbers

for(int i=0; i < numbers.length; i++){

int number = numbers[i];

int reversedNumber = 0;

int temp=0;

/*

* If the number is equal to it’s reversed number, then

* the given number is a palindrome number.

*

* For example, 121 is a palindrome number while 12 is not.

*/

//reverse the number

while(number > 0){

temp = number % 10;

number = number / 10;

reversedNumber = number* 10 + temp;

}

if(numbers[i] == reversedNumber)

System.out.println(numbers[i] + ” is a palindrome number”);

else

System.out.println(numbers[i] + ” is not a palindrome number”);

}

}

}

// Gaurav Mittal

• Tshepo Laespero Mtseda says:

*
**
***
****
*****
****
***
**
*

• Raj Singh Chauhan says:

class Star{

public static void main(String[] ag){

for(int i=0;i<5;i++){

for(int j=0;j<i;j++){ system.out.print(“*”);=”” }=”” system.out.println(“*”);=”” }=”” for(int=”” k=”0;k&lt;5;k++){” for(int=”” l=”0;l&lt;4-k;l++){” system.out.print(“*”);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

• Raj Singh Chauhan says:

class Test{

void show(){
for(int i=0;i<5;i++){

for(int j=0;j<i;j++){ system.out.print(“*”);=”” }=”” system.out.println(“*”);=”” }=”” for(int=”” k=”0;k&lt;5;k++){” for(int=”” l=”0;l&lt;4-k;l++){” system.out.print(“*”);=”” }=”” system.out.println();=”” }=”” }=”” }=”” public=”” class=”” star=”” extends=”” test{=”” public=”” static=”” void=”” main(string[]=”” ag){=”” star=”” s=”new” star();=”” s.show();=”” }=”” }=””>

• simbu says:

public class NewClass1 {

public static void main(String[] args) {

for(int i=1;i<=5;i++)

{

for(int j=1;j<=i;j++)

{

if(i==4)

{

if(j==3 || j==4 || j==5)

{

continue;}}

if(i==5)

{

if(j==2 || j==3 || j==4 || j==5)

{ continue;}}

System.out.print(“*”);

}

System.out.println(“”);

}

}

}

• Ankita says:

In java palindrome number why %10, /10, *10 is used?

• Anirudh Gupta says:

when i enter 121 it says it is not a palindrome

• Max joseph says:

public class Palindrome {

public static void main(String[] args) {

int m=121;

int x=m;

int rev=0;

while(m!=0){

rev=rev*10;

rev=rev+m%10;

m=m/10;
}

if(x==rev){

System.out.println(“The given number “+x+” is palindrome”);

}

else

System.out.println(“The given number “+x+” is not palindrome”);

}

}

• ekowcharles says:

Can someone validate the following as an alternative simpler implementation:

class Palindrome {
public static void main(String… args) {
long number = 0L;

try {
number = new Long(args[0]);
} catch (NumberFormatException ex) {
System.out.println(“Enter a valid number.”);
System.exit(1);
}

//excluding negative values and single positive integers
if (number < 10) {
System.out.println(“Number is not a palindrome.”);
return;
}

long otherNumber = 0;
long temp = number;

while (temp > 0) {
otherNumber = (otherNumber * 10) + (temp % 10);
temp = temp / 10;
}

if (otherNumber == number) {
System.out.println(“Number is a palindrome.”);
} else {
System.out.println(“Number is not a palindrome”);
}
}
}

• gurpreet singh says:

—–*
—-*-*
—*-*-*
–*-*-*-*