package logics;
public class decimalToBinary {
public static void main(String []a)
{
convert(3);
}
public static void convert(int dec)
{
int barray[]=new int[32];
int index=0;
while(dec>0)
{
barray[index++]=dec%2;
dec=dec/2;
}
System.out.println("binary equivalant is");
for(int i=barray.length-1;i>=0;i--)
{
System.out.print(barray[i]);
}
/* converting again into decimal*/
System.out.println("\nDecimal equivalant is");
int sum=0;
int pow=0;
for(int i=0;i<barray.length-1;i++)
{
sum=sum+(barray[i]*(int)Math.pow(2.0,pow ));
pow=pow+1;
}
System.out.print(sum);
}
}//class
output:
--------------------------
binary equivalant is
00000000000000000000000000000011
Decimal equivalant is
3
public class decimalToBinary {
public static void main(String []a)
{
convert(3);
}
public static void convert(int dec)
{
int barray[]=new int[32];
int index=0;
while(dec>0)
{
barray[index++]=dec%2;
dec=dec/2;
}
System.out.println("binary equivalant is");
for(int i=barray.length-1;i>=0;i--)
{
System.out.print(barray[i]);
}
/* converting again into decimal*/
System.out.println("\nDecimal equivalant is");
int sum=0;
int pow=0;
for(int i=0;i<barray.length-1;i++)
{
sum=sum+(barray[i]*(int)Math.pow(2.0,pow ));
pow=pow+1;
}
System.out.print(sum);
}
}//class
output:
--------------------------
binary equivalant is
00000000000000000000000000000011
Decimal equivalant is
3
Không có nhận xét nào:
Đăng nhận xét