Check if a number is Palindrome
Given an integer, write a function that returns true if the given number is a palindrome, else false. For example, 12321 is a palindrome, but 1451 is not a palindrome.
Solution 1st: Simplest
https://ide.geeksforgeeks.org/AJE7SI37TS
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
int input, output, temp, remainder;
output=0;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
temp = input;
while(input != 0){
remainder = input % 10;
output = (output * 10) + remainder;
input = input/10;
}
if(output==temp)
System.out.println(output+ " is a Palindrome");
else
System.out.println(output+ " is not a Palindrome");
}
}
Solution 2nd: Out of the box
https://ide.geeksforgeeks.org/tVpfTkXbCM
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
StringBuilder strInput = new StringBuilder();
strInput.append(input);
StringBuilder strOut= strInput.reverse();
if(strInput==strOut)
System.out.println(input+ " is a Palindrome");
else
System.out.println(input+ " is not a Palindrome");
}
}
Solution 1st: Simplest
https://ide.geeksforgeeks.org/AJE7SI37TS
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
int input, output, temp, remainder;
output=0;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
temp = input;
while(input != 0){
remainder = input % 10;
output = (output * 10) + remainder;
input = input/10;
}
if(output==temp)
System.out.println(output+ " is a Palindrome");
else
System.out.println(output+ " is not a Palindrome");
}
}
Solution 2nd: Out of the box
https://ide.geeksforgeeks.org/tVpfTkXbCM
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
StringBuilder strInput = new StringBuilder();
strInput.append(input);
StringBuilder strOut= strInput.reverse();
if(strInput==strOut)
System.out.println(input+ " is a Palindrome");
else
System.out.println(input+ " is not a Palindrome");
}
}
Comments
Post a Comment