July 10, 2022 |18.6K Views

    How to Reverse a Number in Java

      Share   Like
    Description
    Discussion

    In this video, we will learn the Java program on how to reverse a number.

    Here, we reverse a number using two different methods:
    1. Using While loop
    2. Using recursion function

    A loop is a part of a computer program that allows a certain set of instructions to be executed repeatedly. A loop is created by enclosing the set of instructions inside a block of code called a bracket and throwing in the keyword while.

    Using While loop:
    1) Take the number’s modulo by 10.
    2) Multiply the reverse number by 10 and add a modulo value to the reverse number.
    3) Divide the number by 10.
    4) Repeat the above steps until the number becomes zero.

    Using recursion function:
    The recursion operator in Java allows you to repeat an instruction or statement a set number of times.
    1) If the number becomes zero then terminate the recursion, this will be the base condition.
    2) Take the modulo and add it with the ‘rev*10’ multiplying.
    3) Divide the number by 10 and call the reverse function on this number after updating it to number/10.

    Java Program to Reverse a Number: https://www.geeksforgeeks.org/java-program-to-reverse-a-number/