Vous êtes sur la page 1sur 6

Experiment No.

1

Aim:
To convert decimal number to binary number and vice versa.

Software Used:
Java Software Development Kit.

Explanation:
The decimal number system has ten possible values(0-9). In contrast, the binary number system has two
possible values, often represented as 0 or 1.

Decimal to Binary:
To convert decimal number to binary number:
1. Consider the integral part.
2. Successively divide by 2 till the quotient is greater than 1.
3. For each division, note the mod 2 value.
4. Read the mod 2 valmues from bottom to top to get the integral binary number.
5. Now, consider the fractional part.
6. Multiply the fractional part by 2.
7. If the value is less than 1, note 0 else if the value is greater than or equal to 1, note 1 and then
subtract 1 from the result.
8. Continue this operation till the result becomes 0 or the counter becomes 5, whichever comes
first.
9. Read the values from top to bottom to get the fractional binary number.

Binary to Decimal:
To convert binary number to decimal number:
1. Consider the integral part.
2. For each place value, multiply with powers of 2 - 2
0
for LSB, 2
1
for the bit to its left and so on.
3. Consider the fractional part.
4. For each place value, multiply with negative powers of 2 2
-1
for first place after binary point, 2
-
2
for second place after binary point and so on.
5. Add all the values to get the desired decimal number.


Examples:

Decimal to Binary:

(28.75)
10
= (?)
2

2 28 0
2 14 0
2 7 1
2 3 1
2 1 1

0.75 * 2 = 1.50 1
0.50 * 2 = 1.00 1

(28.75)
10
= (11100.11)
2

Binary to Decimal:

(11100.11)
2
= (?)
10

11100.11
= 1 * 2
4
+ 1 * 2
3
+ 1 * 2
2
+ 0 * 2
1
+ 1 * 2
0
+ 1 * 2
-1
+ 1 * 2
-2

= 16 + 8 + 4 + 0.5 + 0.25
= 28.75

(11100.11)
2
= (28.75)
10










Program:

To convert decimal number to binary number.

import java.util.Scanner;
class DecimalToBinary
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double decimal;
String binary = "";
System.out.print("Enter decimal number : ");
decimal = sc.nextDouble();
int i = (int)decimal;
System.out.println();
int bit;

while(i > 0)
{
bit = i % 2;
binary = Integer.toString(bit) + binary;
System.out.println("2 | " + i + "\t" + bit);
System.out.println("--|----");
i = i / 2;
}
System.out.println(" |\n");
binary += ".";
double d = decimal - (int)decimal;

for(int j = 0; j < 5; j++)
{
System.out.print(d + " * 2 = ");
d = d * 2;
System.out.print(d);
if(d < 1.0)
{
binary += Integer.toString(0);
System.out.print("\t 0");
}
else
{
binary += Integer.toString(1);
d -= 1;
System.out.print("\t 1");
if(d == 0.0)
break;
}
System.out.println();
}
System.out.println("\n"+decimal +" in binary form is " +binary);
}
}

Output:







Program:

To convert binary number to decimal number.

import java.util.Scanner;

class BinaryToDecimal
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String b;
String str = "";
System.out.print("Enter binary number : ");
b = sc.next();
int digit = 0;
int decimal = 0;
int point = b.indexOf(".");
String binary = b.substring(0, point);
System.out.println(b);
System.out.print("= ");

for(int i = binary.length() - 1, pos = 0; i >= 0 ; i--, pos++)
{
digit = Integer.parseInt(binary.charAt(i) + "");
decimal += digit * Math.pow(2,pos);
str = Double.toString(digit * Math.pow(2,pos)) + " + " + str;
}
System.out.print(str);
double d = 0.0;
binary = b.substring(point + 1, b.length());
for(int i = 0; i < binary.length() ; i++)
{
digit = Integer.parseInt(binary.charAt(i) + "");
d += digit / Math.pow(2, i+1);
System.out.print(digit / Math.pow(2, i+1));
if(i != binary.length() - 1)
System.out.print(" + ");
}
d += decimal;
System.out.println("\n= " + d);
System.out.println(b +" in decimal form is " + d);
}
}

Output:



Conclusion:
Decimal to binary conversion and vice versa is used in computer systems as computers store data in
binary format and humans understand numbers in decimal format.

Vous aimerez peut-être aussi