Programming Examples

Java recursive function to reverse the digits of number


Write java program to input a number and reverse the digits of number by using recursive function.

import java.util.*;
class Revese
{
    public static int rev(int n,int r)
    {
        if(n==0)     
        {
            return r;
        }
        else
        {
            r=r*10+n%10;
            return(rev(n/10,r));
        }
    }
    public static void main(String arr[]) 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any Number to Reverse: ");
        int num=sc.nextInt();
        int res=rev(num,0);
        System.out.println("Rev of Number is "+res);
    }
}
Output

Enter any Number to Reverse: 

345

Rev of Number is 543

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz