In the previous article, we have discussed Java Program to Check Spy Number
In this article we are going to understand what Reverse number is and how we can check whether a number is Reverse or not in Java with examples.
Program to Find Reverse Number of a Given Number
Reverse numbers are numbers formed after reversing the digits
Example : 19 =>91 46 =>64
In the above examples the reverse of 19 and 46 are 91 and 64 respectively.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach :
- We ask the user to enter a number and store it.
- We iterate through its digits inside a loop and store them in another variable.
- The reverse number inside the variable is printed.
Let’s see different ways to find reverse of a number.
Method-1: Java Program to find Reverse Number of a Given Number By Using Static Value
import java.util.Scanner; public class ReverseNumber { public static void main(String args[]) { //number declared; int num = 67; int rev=0,remainder,temp=num; while(temp>0) { remainder=temp%10; rev = rev*10+remainder; temp=temp/10; } System.out.println("The reverse of "+num+" is "+rev); } }
Output: The reverse of 67 is 76
Method-2: Java Program to find Reverse Number of a Given Number By User Input Value
import java.util.Scanner; public class ReverseNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int rev=0,remainder,temp=num; while(temp>0) { remainder=temp%10; rev = rev*10+remainder; temp=temp/10; } System.out.println("The reverse of "+num+" is "+rev); } }
Output: Enter a number : 45 The reverse of 45 is 54
By C Programming Language:
#include <stdio.h> int main() { int number, reverse = 0, remainder; printf("Enter an integer: "); //taking anumber inputfrom user scanf("%d", &number); //Reversing anumber while (number != 0) { remainder = number % 10; reverse = reverse * 10 + remainder; number /= 10; } printf("Reversed number: %d", reverse); return 0; }
Output: Enter an integer: 78 Reversed number = 87
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: