- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to print the reverse of the given number
Following is the algorithm to reverse a given number.
Algorithm
1. Get the number to reverse. 2. Hold the number in temporary variable. 3. Start the while loop with condition temp >0. 4. Store the first digit in the temporary variable d by performing modulus operation on temp with 10. 5. Multiply the revnum (initialized with 0) with 10 and concatenate the digit obtained in the previous step. 6. Reduce one digit in the temp by dividing with 10.
Example
import java.util.Scanner; public class ReverseOfANumber { public static void main(String args[]) { int d, number,temp, revnum = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter a number ::"); number = sc.nextInt(); temp = number; while (temp >0) { d = temp %10; revnum = (revnum*10)+d; temp = temp/10; } System.out.println("Reverse of the given number is:"+revnum); } }
Output
Enter a number :: 5112115 Reverse of the given number is:5112115
Advertisements