import java.io.*;
public class BaseConversion
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static String Orig_Num="",Conv_Num="",KeyNodes="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static int Orig_Base=0,Conv_Base=0,Deci_Num=0,p=0,flag=0;
private void input()throws Exception
{
do
{ System.out.print("Enter the ORIGINAL NUMBER (ALPHA IN CAPS ONLY) : ");Orig_Num=br.readLine();
Orig_Num=Orig_Num.toUpperCase();
do
{ flag=0;
System.out.print("Enter the ORIGINAL Base : ");Orig_Base=Integer.parseInt(br.readLine());
if((Orig_Base<2)||(Orig_Base>36))
{System.out.println("Invalid Base");flag=1;}
}while(flag==1);
if(!validate(Orig_Num,Orig_Base))
{System.out.println("Invalid Character Of Valid Base Found");flag=1;}
}while(flag==1);
do
{ flag=0;
System.out.print("Enter the CONVERSION Base : ");Conv_Base=Integer.parseInt(br.readLine());
if((Conv_Base<2)||(Conv_Base>36))
{System.out.println("Invalid Base");flag=1;}
}while(flag==1);
}
private boolean validate(String Num,int Base)
{
for(int i=0;i<Num.length();i++)
if(KeyNodes.indexOf(Num.charAt(i))>=Base) return false;
return true;
}
private void decimalConvert()
{
p=Orig_Num.length()-1;
for(int i=0;i<Orig_Num.length();i++)
Deci_Num+=((KeyNodes.indexOf(Orig_Num.charAt(i)))*(Math.pow(Orig_Base,p--)));
}
private void baseConvert()
{
do
{ Conv_Num=(KeyNodes.charAt(Deci_Num%Conv_Base)+Conv_Num);
Deci_Num/=Conv_Base;
}while(Deci_Num>=1);
}
public static void main(String args[])throws Exception
{
BaseConversion obj=new BaseConversion();
obj.input();obj.decimalConvert();if(!(Conv_Base==10))obj.baseConvert();
System.out.println("\fOriginal Number : "+Orig_Num+" :: Base : "+Orig_Base);
if(!(Conv_Base==10)) System.out.println("Converted Number : "+Conv_Num+" :: Base : "+Conv_Base);
else System.out.println("Converted Number : "+Deci_Num+" :: Base : "+Conv_Base);
}
}
public class BaseConversion
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static String Orig_Num="",Conv_Num="",KeyNodes="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static int Orig_Base=0,Conv_Base=0,Deci_Num=0,p=0,flag=0;
private void input()throws Exception
{
do
{ System.out.print("Enter the ORIGINAL NUMBER (ALPHA IN CAPS ONLY) : ");Orig_Num=br.readLine();
Orig_Num=Orig_Num.toUpperCase();
do
{ flag=0;
System.out.print("Enter the ORIGINAL Base : ");Orig_Base=Integer.parseInt(br.readLine());
if((Orig_Base<2)||(Orig_Base>36))
{System.out.println("Invalid Base");flag=1;}
}while(flag==1);
if(!validate(Orig_Num,Orig_Base))
{System.out.println("Invalid Character Of Valid Base Found");flag=1;}
}while(flag==1);
do
{ flag=0;
System.out.print("Enter the CONVERSION Base : ");Conv_Base=Integer.parseInt(br.readLine());
if((Conv_Base<2)||(Conv_Base>36))
{System.out.println("Invalid Base");flag=1;}
}while(flag==1);
}
private boolean validate(String Num,int Base)
{
for(int i=0;i<Num.length();i++)
if(KeyNodes.indexOf(Num.charAt(i))>=Base) return false;
return true;
}
private void decimalConvert()
{
p=Orig_Num.length()-1;
for(int i=0;i<Orig_Num.length();i++)
Deci_Num+=((KeyNodes.indexOf(Orig_Num.charAt(i)))*(Math.pow(Orig_Base,p--)));
}
private void baseConvert()
{
do
{ Conv_Num=(KeyNodes.charAt(Deci_Num%Conv_Base)+Conv_Num);
Deci_Num/=Conv_Base;
}while(Deci_Num>=1);
}
public static void main(String args[])throws Exception
{
BaseConversion obj=new BaseConversion();
obj.input();obj.decimalConvert();if(!(Conv_Base==10))obj.baseConvert();
System.out.println("\fOriginal Number : "+Orig_Num+" :: Base : "+Orig_Base);
if(!(Conv_Base==10)) System.out.println("Converted Number : "+Conv_Num+" :: Base : "+Conv_Base);
else System.out.println("Converted Number : "+Deci_Num+" :: Base : "+Conv_Base);
}
}
No comments:
Post a Comment