-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Sunday, September 20, 2020

Java Program To Reverse a Number using Recursion

  • September 20, 2020
  • by

 

Java Program To Reverse a Number using Recursion

In this Java Program, we reverse a number using Recursion.

The process in which the function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function

Step 1 : Import Scanner Class

Step 2 : Create a method for reverse

Step 3 : Apply if condition in which the number should be smaller than 10

Step 4 : Print the number

Step 5 : Apply else condition in which number should mod by 10 and print the result

Step 6 : Apply reverse method condition in which number should be divided by 10

Step 7 : Create main method

Step 8 : Initialize num to 0

Step 9 : As the user to enter the number and press enter

Step 10 : Create Scanner object

Step 11 : Store the user entered number into num

Step 12 : Print Reverse of the input number is.

Step 13 : Call the reverseMethod method

Program :

import java.util.Scanner;

public class RecursionReverseDemo 

    {   

        //A method for reverse

        public static void reverseMethod(int number)

        {

            if(number < 10)

            {

                System.out.println(number);

                return;

            }

            else

            {

                System.out.print(number % 10);

                //Method is calling itself : recursion

                reverseMethod(number / 10);

            }

        }

        public static void main(String args[])

        {

            int num = 0;

            System.out.println("Input your number and press enter:");

            Scanner sc = new Scanner(System.in);

            num = sc.nextInt();

            System.out.println("Reverse of the input number is:");

            reverseMethod(num);

            System.out.println();

        }        

}

Output :

Input your number and press enter:

5678901

Reverse of the input number is:

1098765

For execution of the same do visit my YouTube Channel : 

https://youtu.be/uu50BfxfhjA



A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages