In: Computer Science
input code:
output:
code:
import java.util.*;
import java.lang.*;
class Convertor
{
/*make method for getting a decimal*/
public static String decimal2Binary(int n)
{
/*declare the String*/
String str1="";
/*take a loop*/
while(n>0)
{
/*find reminder*/
int r=n%2;
n=n/2;
/*add into binary*/
str1=(char)(r+'0')+str1;
}
/*return binary*/
return str1;
}
/*make method for gettomg decimal*/
public static int binary2Decimal(String bin)
{
/*declare the variable*/
int dec=0;
/*take a loop*/
for(int i=0;i<bin.length();i++)
{
/*calculate decimal*/
dec+=Character.getNumericValue(bin.charAt(i))*Math.pow(2,bin.length()-i-1);
}
/*return decimal*/
return dec;
}
}
/*driver class*/
public class Main
{
public static void main(String[] args)
{
/*call a function*/
System.out.println("Output of 110 to decimal=
"+Convertor.binary2Decimal("110"));
System.out.println("Output of 5 to binary=
"+Convertor.decimal2Binary(5));
}
}