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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 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 Best Website and Software Company in Allahabad