Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Friday 30 November 2018

Palindrome (Number and word) in java


Word-


import java.util.Scanner;

class GFG {
public static void main (String[] args) {
//System.out.println("GfG!");
Scanner scan = new Scanner(System.in);
String word = scan.next();
StringBuffer sb = new StringBuffer(word);
String wordReverse = new String(sb.reverse());
if (word.equals(wordReverse))
System.out.println("yes");
else
    System.out.println("no");

}
}



Number -




import java.util.Scanner;

class GFG {
public static void main (String[] args) {
//System.out.println("GfG!");
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
String numberString = Integer.toString(number);

StringBuffer sb = new StringBuffer(numberString);
String numberReverse = new String(sb.reverse());
if (numberString.equals(numberReverse))
System.out.println("yes");
else
    System.out.println("no");

}
}

No comments:

Post a Comment