×

Java Programs

Java Practice

Java program to Reverse an Integer Number

Reverse Number program in Java - This program will read and integer number from the user and prints the Reverse Number of given integer number.

Reverse Number using Java program

//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);

  }
}

Output

    
    me@linux:~$ javac ReverseNumber.java 
    me@linux:~$ java ReverseNumber 

    Enter an integer number: 12345
    Reverse Number is: 54321

Using Function/Method

//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);

  }
}

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.