Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
78 views

Java Program To Reverse Number

This document provides the code for a Java program that reverses a number. The program takes a number as input, uses a while loop to iterate through each digit of the number, extracts the last digit and stores it in the reverse variable after multiplying it by 10, and divides the input number by 10 to remove the last digit. After the loop, the reverse variable contains the reversed number which is then printed as output. The document also mentions that recursion can be used to reverse a number and this code can check if a number is a palindrome.

Uploaded by

vinchooo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Java Program To Reverse Number

This document provides the code for a Java program that reverses a number. The program takes a number as input, uses a while loop to iterate through each digit of the number, extracts the last digit and stores it in the reverse variable after multiplying it by 10, and divides the input number by 10 to remove the last digit. After the loop, the reverse variable contains the reversed number which is then printed as output. The document also mentions that recursion can be used to reverse a number and this code can check if a number is a palindrome.

Uploaded by

vinchooo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Program To

Reverse Number

http://freepdf-books.com
This program prints reverse of a number i.e.
if the input is 951 then output will be 159.
Java programming source code
import java.util.Scanner;

class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;

System.out.println("Enter the number to reverse");


Scanner in = new Scanner(System.in);
n = in.nextInt();

while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

System.out.println("Reverse of entered number is


"+reverse);
}
}

Download Reverse number program class


file.

http://freepdf-books.com
Output of program:

You can also reverse or invert a number


using recursion. You can use this code to
check if a number is palindrome or not, if
the reverse of an integer is equal to integer
then it's a palindrome number else not.

http://freepdf-books.com

You might also like