ADVERTISEMENT
ADVERTISEMENT

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:

    
    [email protected]:~$ javac ReverseNumber.java 
    [email protected]:~$ 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

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.