Computer Science, asked by gobindabanik13, 3 months ago

wap to print all the four digit palindrome number in java​

Answers

Answered by anindyaadhikari13
2

Required Answer:-

Question:

  • Write a program to print all the four digit palindrome numbers in Java.

Solution:

Here is the program.

public class Palindrome {

public static void main(String[] args) {

System.out.println("All Palindrome numbers are...");

for(int i=1000;i<=9999;i++) {

if(isPalindrome(i))

System.out.println(i);

}

}

static boolean isPalindrome(int n) {

int m=n, s=0;

while(m!=0) {

s=s*10+m%10;

m/=10;

}

return (s==n);

}

}

Explanation:

  • The isPalindrome() function checks whether a number is palindrome or not.
  • We have created a loop that iterates from i = 1000 to 9999. Inside the loop, we will check if a number is palindrome or not using isPalindrome() function which we have created earlier.

Output is attached.

Attachments:
Similar questions