Java Program to Reverse an Integer Number

You are currently viewing Java Program to Reverse an Integer Number

Java Program to Reverse an Integer Number

//Java program to Reverse a Number.

import java.util.*;

public class ReverseNumber {
  public static void main(String[] args) {
    int number;

    Scanner sc = new Scanner(System.in);

    //Read Number
    System.out.print("Enter an integer number: ");
    number = sc.nextInt();

    //calculate reverse number
    int reverse_number = 0;
    while (number > 0) {
      reverse_number = (reverse_number * 10) + number % 10;
      number /= 10;
    }

    System.out.println("Reverse Number is: " + reverse_number);

  }
}
//Java program to Reverse a Number.

import java.util.*;

public class ReverseNumber {
  //Function to find Reverse Number
  public static int RevNumber(int num) {
    int sum = 0;
    while (num > 0) {
      sum = (sum * 10) + num % 10;
      num /= 10;
    }
    return sum;
  }
  public static void main(String[] args) {
    int number;

    Scanner sc = new Scanner(System.in);

    //Read Number
    System.out.print("Enter an integer number: ");
    number = sc.nextInt();

    //find reverse number - calling function
    int reverse_number;
    reverse_number = RevNumber(number);

    System.out.println("Reverse Number is: " + reverse_number);

  }
}

Output

    
    [email protected]:~$ javac ReverseNumber.java 
    [email protected]:~$ java ReverseNumber 

    Enter an integer number: 12345
    Reverse Number is: 54321

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply