1. Overview
In this tutorial, you'll learn how to reverse a number in java in simple way using for loop and while loop.
You should have the understanding of basics for loop and while loop.
If you are new to the java programming, learn how to print the hello world program.
Example:
Input: 1234
Output: 4321
Reversing the number means taking each digit from last and adding the same at the first in output.
2. Example 1 - Reversing the number using For Loop
Let us first write a simple program that reverse the number using for loop running till number != 0.
For loop has three sections.
Step 1: Initialization
Step 2: Condition evaluation
Step 3: Increment or decrement
Here, you need to remember that step 1 and step 3 are optional but the condition is mandatory. Because for loop run till the condition is satisfied.
So, reversing a number logic is run till the condition is true.
public class ReverseNumberForLoopexample { public static void main(String[] args) { // creating a input number type integer int inputNumber = 1234; // printing the input number System.out.println("Input Number : " + inputNumber); // creating a variable to hold the reversed value. int outputReversedNumber = 0; for (; inputNumber != 0;) { // taking last digit from number int lastDigit = inputNumber % 10; // adding last digit to the output value. outputReversedNumber = outputReversedNumber * 10 + lastDigit; // removing the last digit from number. inputNumber = inputNumber / 10; } // printing the output number after successful reversal. System.out.println("Reversed number : " + outputReversedNumber); } }
Output:
Input Number : 1234 Reversed number : 4321
First, read the inline comments in the program to understand each line the program.
Essentially, core logic is to take the last digit from input number and add it to the output number. After adding last digit to the output number then need to remove it from input number using inputNumber = inputNumber / 10; statement.
Repeat the same logic until inputNumber is not 0. If this condition is failed then input is reversed.
Finally, output number is having the completely reversed number.
3. Example 2 - Reversing the number using While Loop
Next, let us try the same example with the while loop. To reverse a number while loop needs only a condition.
package com.javaprogramto.programs.numbers; /** * Reversing a number using while loop in java. * * Example: input- 12345, output - 54321 * * @author javaprogramto.com * */ public class ReverseNumberWhileLoopexample { public static void main(String[] args) { // creating a input number type integer int inputNumber = 12345; // printing the input number System.out.println("Input Number : " + inputNumber); // creating a variable to hold the reversed value. int outputReversedNumber = 0; while (inputNumber != 0) { // taking last digit from number int lastDigit = inputNumber % 10; // adding last digit to the output value. outputReversedNumber = outputReversedNumber * 10 + lastDigit; // removing the last digit from number. inputNumber = inputNumber / 10; } // printing the output number after successful reversal. System.out.println("Reversed number : " + outputReversedNumber); } }
Output:
Input Number : 12345 Reversed number : 54321
In the above program, last digit is removed in each iteration and that removed digit is added to the output number. While loop runs till the number is not 0 and l.
4. Conclusion
In this article, you've seen the example programs to reverse a number with explanation.
No comments:
Post a Comment
Please do not add any spam links in the comments section.