Recursive program to convert a decimal number to any base.

Recursive program to convert a decimal number to any base.
import java.util.*;
class dectoanyrec{
    public String anyB(int n,int b){
        if(n<b)
        return n<=9?""+n:""+(char)(n+55);
        else
        return anyB(n/b,b)+(n%b<=9?""+n%b:(char)(n%b+55));
    }//anyB
    public static void main(){
         Scanner sc=new Scanner(System.in);
        System.out.println("Enter any decimal number");
        int n=sc.nextInt();
        System.out.println("Enter the base");
        int b=sc.nextInt();
        dectoanyrec DR=new dectoanyrec();
        System.out.println("The number in the base  "+b+"  Is  "+DR.anyB(n,b));
    }//main
}//class
 OUTPUT---
Enter any decimal number
246824
Enter the base
3
The number in the base  3  Is  110112120122

Comments

Popular posts from this blog

Sort Boundary Elements Of A Matrix

Lucky Number In Java ISC

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.