Computer Science, asked by Tanichakraborty, 8 months ago

Java programming


1. write a program to check for palindrome number from 2000 to 3000 .
2. Write a program to check whether a given string is a palindrome string or not ?

please help me i can't able to solve this program ​

Answers

Answered by anindyaadhikari13
2

Question 1 :-

Write a program to check for palindrome number from 2000 to 3000.

Program:-

class Palindrome

{

static boolean isPalindrome(int n)

{

int n1=n, s=0;

for(;n1!=0;n1/=10)

s=s*10+n1%10;

return (s==n);

}

public static void main(String args[])

{

System.out.println("Palindrome numbers between 2000 to 3000 are... ");

for(int i=2000;i<=3000;i++)

{

if(isPalindrome(i))

System.out.print(i+" ");

}

}

}

Explanation:-

Here, I have created a return type method which checks if the reverse of the number is equal to the original number. If they are equal they the method returns true else false.

Now, inside the main(), I have created a for loop and inside the for loop, I have used the method to check for palindrome numbers and then, they are printed.

Question 2:-

  • Write a Java program to check weather a string is palindrome or not.

Program:-

import java.util.*;

class Palindrome

(

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter a String: ");

String s=sc.nextLine();

String n="";

int length=s.length();

for(int i=length-1;i>=0;i--)

{

char ch=s.charAt(i);

n+=ch;

}

if(s.equalsIgnoreCase(n)==true)

System.out.println("String is a Palindrome String.");

else

System.out.println("String is not a Palindrome String. ");

}

}

Explanation:-

This is similar to the previous question. I have created two string variables. I have taken input from user and then reversed the String and using the method equalIgnoreCase(), I have checked whether the strings are equal or not(ignoring case) and if the are equal, message is displayed that "String is Palindrome."

Similar questions