0
create a java program to test if a given int num is a pallindrome
2 Réponses
0
I have done this program in c# so I don't know if java can do the same. My guess is it can. So basically the way I did it is
1)have user input that number
2)convert it into a string
(or you could have simply made the user input that number as a string)
3)i=0; j=stringNumber.Length-1
(where I is the first position in the number eg. in the number 1234 I is 1, and j is the last position in the number in this example j is 4)
3.5)Declare a bool that returns true if number is palindrome and false if its not
bool isPalindrome;
4)have a while(i<=j)
5)inside the while you will have an if statement that goes like this
if(stringNumber[i]!=stringNumber[j])
{
isPalindrome = false;
break;
}
else
isPalindrome = true;
i++;
j--;
Here is the entire code in c#
https://code.sololearn.com/cfcE6UdPt0Pv/?ref=app
I hope I helped ;)
0
The java program to check whether the number is palindrome or not
public class Palindrome {
static void Pal(int n)
{
int div=0,rev=0,temp=n;
while(temp>0)
{
div=temp%10;
rev=rev*10+div;
temp=temp/10;
}
System.out.println("the palindrome is:" + rev);
if(n==rev)
{
System.out.println("the number is palindrome");
}
else
{
System.out.println("the number is not palindrome");
}
}
public static void main(String[] args) {
System.out.println("enter the number:");
Scanner s= new Scanner(System.in);
int n=s.nextInt();
Pal(n);
}
}