Aim:- Write a program in java to convert a number from binary to its decimal equivalent.
Software used:- Notepad, Command prompt
Program coding:-
public class bd
{
public static void main(String[] args)
{
int r,n,bin=0,p=0;
n=Integer.parseInt(args[0]);
do
{
r=n%10;
n=(int)n/10;
bin=bin+r*(int)Math.pow(2,p);
p++;
}
while(n>0);
n=Integer.parseInt(args[0]);
System.out.println("Decimal number is "+ bin);
}
}
Output:-
Aim:- Write a program in java to convert a number from decimal to its binary equivalent.
Software used:- Notepad, Command prompt
Program coding:-
public class db
{
public static void main(String[] args)
{
int r,n,bin=0,p=0;
n=Integer.parseInt(args[0]);
do
{
r=n%2;
n=(int)n/2;
bin=bin+r*(int)Math.pow(10,p);
p++;
}
while(n>0);
n=Integer.parseInt(args[0]);
System.out.println("Binary number is "+ bin);
}
}


0 Comments
if you have any problem, please let me know