Java Exercises: Check if a positive number is a palindrome or not
Java Basic: Exercise-115 with Solution
Write a Java program to check if a positive number is a palindrome or not.
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.*;
public class test {
public static void main(String[] args)
{
int num;
Scanner in = new Scanner(System.in);
System.out.print("Input a positive integer: ");
int n = in.nextInt();
System.out.printf("Is %d is a palindrome number?\n",n);
System.out.println(is_Palindrome(n));
}
public static int reverse_nums(int n) {
int reverse = 0;
while (n != 0) {
reverse *= 10;
reverse += n % 10;
n /= 10;
}
return reverse;
}
public static boolean is_Palindrome(int n) {
return (n == reverse_nums(n));
}
}
Sample Output:
Input a positive integer: 151 Is 151 is a palindrome number? true
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to given a string and an offset, rotate string by offset (rotate from left to right).
Next: Write a Java program which iterates the integers from 1 to 100. For multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". When number is divided by both three and five, print "fizz buzz".
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework