Ticker

6/recent/ticker-posts

Header Ads Widget

Reverse of a number in java

Reverse of a number in java


Introduction
In this tutorial we gonna learn ,how to reverse of a number in java and check whether the string is planidrome or not.

Before going further lets understand what planidrome is.

Q) What is Planidrome?
Sol) A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward.

Eg :- madam ,   1001 etc

This is a simple program in which we first declare a variable first and then scan it ,when user give it input and then apply logic for reversing it and give it as output.so without waisting time lets see how our this idea look like in real.


Q) Write a java program to reverse of a number?

Sol)



import java.util.Scanner;
public class Main {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n, num, digit, rev = 0;

    System.out.printf( "Enter a positive number: ");
     num = in.nextInt();

     n = num;

     do
     {
         digit = num % 10;
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);

     System.out.print(" The reverse of the number is: " + rev + "\n");

     if (n == rev)
       System.out.printf(" The number is a palindrome.");
     else
         System.out.printf(" The number is not a palindrome.");

    

}
}



So ,this is how our concept look like and now its time to see how output will be like.i am sure will be eager like me to see how its its  output.

Output 1 :

Enter a positive number: 12321
 The reverse of the number is: 12321
 The number is a palindrome.



Output 2:


Enter a positive number: 12345
 The reverse of the number is: 54321
 The number is not a palindrome.



Post a Comment

0 Comments