In this program, user will enter any decimal number and the base value( 2 for binary, 7 for octal and 16 for hexadecimal). Program will display the converted value in that particular numbering system.
Codes of the program
import java.io.*;
class Convert
{
int n,b;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Convert()
{
n=0;
b=0;
}
public void read_n_b() throws IOException
{
System.out.println("Enter the decimal Number:");
n=Integer.parseInt(br.readLine());
while(true)
{
System.out.println("Enter the base( 2-16):");
b=Integer.parseInt(br.readLine());
if(b==2 || b==7 || b==16)
break;
}
getAns(n,b);
}
private void getAns(int x,int y)
{
int c=x%y;
x=x/y;
if(x==0)
{
System.out.print(c);
return;
}
getAns(x,y);
if(c >9)
{
switch(c)
{
case 10:
System.out.print("A");
break;
case 11:
System.out.print("B");
break;
case 12:
System.out.print("C");
break;
case 13:
System.out.print("D");
break;
case 14:
System.out.print("E");
break;
case 15:
System.out.print("F");
break;
}
}
else
System.out.print(c);
}
public static void main(String args[]) throws IOException
{
Convert ob=new Convert();
ob.read_n_b();
}
}
Here read_n_b () function takes the decimal number and the base value. To ensure the proper entry of base value, an infinite loop is used. Next the values are passed to the function ‘private void getAns()’.
This function is called recursively to show the output and this is trail recursion where pending statements creates stack. In this program, the pending statements are those used to display the value in specific format of numbering system.
Related Post: BlueJ Programs on Number
Related Post: BlueJ Programs on Number
Thank You....
ReplyDeleteI have a another problem...
WAP to find all the combination of a word.
For e.g.
Input-CAT
Output-
CAT
TAC
ACT
ATC
TCA
CTA
This program has been posted in my site. Please search it.
ReplyDeleteSir,can you suggest the kind of questions that comes from stacks and Queues in ISC.........
ReplyDeleteI have never seen any program on stack or queue in ISC papers. Definition of stack and queue is sufficient. You can do programs on use of array as stack and queue.
Delete