Write program that does binary to decimal and decimal to binary conversions.
Program:
package bi_de;
public class Bi_De {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter Binary number=");
int d=sc.nextInt();
System.out.print("Number is Convert into decimal: ");
decimal(d);
System.out.println();
System.out.println();
System.out.print("Enter Decimal number=");
int b=sc.nextInt();
System.out.print("Number is Convert into binary:");
binary(b);
System.out.println("\n");
}
static void binary(int b){
if(b>0)
binary(b/2);
System.out.print(b%2);
}
static void decimal(int d){
int num=0;
double sum=0,rem;
while(d!=0)
{
rem=d%10;
d=d/10;
sum=sum+rem*(Math.pow(2,num));
num++;
}
System.out.print(sum);
}
}
No comments:
Post a Comment