Вы находитесь на странице: 1из 10

Date :

Assignment Number : 06
Problem Statement
Program in C to convert a decimal number into its equivalent
binary number and vice versa using Switch Case technique.

Theory
The decimal numeral system (also called base ten or
occasionally denary) has ten as its base. While
in mathematics and digital electronics, a binary number is
a number expressed in the binary numeral system, or base-2
numeral system, which represents numeric values using two
different symbols: typically 0 (zero) and 1 (one). Conversion of
binary to decimal (base-2 to base-10) numbers and back is an
important concept to understand as the binary numbering system
forms the basis for all computer and digital systems.

Decimal to Binary Conversion


To convert decimal to binary is very simple, we simply divide the
decimal value by 2 and then write down the remainder. Repeat
this process until we cannot divide by 2 anymore, for example
let's take the decimal value 157:
157 2 =
78
78 2 =
39
39 2 =
19
19 2 =
9
92=4
42=2
Page No. :

with
of 1
with
of 0
with
of 1
with
of 1
with
of 1

a remainder
a remainder
a remainder
a remainder
a remainder

<--- to convert write this


remainder first.

with a remainder
of 0
2 2 = 1 with a remainder
1 2 = 0 of 0
with a remainder
of 1
Next, write down the value of the remainders from bottom to top
(in other words write down the bottom remainder first and work
your way up the list) which gives: 10011101 = 157

Binary to Decimal Conversion


To convert binary into decimal is very simple and can be done as
shown below:
Say we want to convert the 8 bit value 10011101 into a decimal
value, we can use a formula table like that below:
12 6 3 1
8 4 2 1
8 4 2 6
1

0 0 1 1 1 0 1

As you can see, we have placed the numbers 1, 2, 4, 8, 16, 32,


64, 128 (powers of two) in reverse numerical order, and then
written
the
binary
value
below.
To convert, we simply take a value from the top row wherever
there is a 1 below and then add the values together. For instance,
in our example we would have 128+16+8+4+1=157.
For a 16 bit value you would use the decimal values 1, 2, 4, 8, 16,
32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
(powers
of
two)
for
the
conversion.
Because we know binary is base 2 then the above could be
written
as:
1*27 + 0*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 1*20 = 157.

Page No. :

Algorithm
Variable description :
Variables
used
ch
c
de
a[]
i
j
bin
b
dec
d
k

Datatype
Integer
Character
Integer
Integer
Integer
Integer
Long Integer
Long Integer
Long Integer
Long Integer
Long Integer

Purpose in the program


Stores the user choice
Stores the user choice
Stores the decimal number to be
converted
Stores the remainder values
Counter variable
Loop variable
Stores the binary number to be
converted
Stores the copy of the binary
number
Stores the decimal number thus
calculated
Stores the remainder
Counter variable

Step 1 : (i)prints "1:Decimal To Binary"


(ii)prints "2:Binary To Decimal"

Page No. :

(iii)prints "3:Exit"
Step 2 : Input the value of ch
Step 3 : Read ch
Step 4 : Start Switch Case
Step 5: (i)for case 1
(ii)Input the value of de
(iii)Read de
(iv)while(de>0)
(v)a[i]=de%2
(vi)i++
(vii)de=de/2
(viii)end while
(ix)for j=i-1 to 0
(x)print a[j]
(xi)break
(xii)end case
Step 6 : (i)for case 2
(ii)Input the value of bin
(iii)Read bin
(iv)b=bin
(v)while(b>0)
(vi)d=b%10
(vii)dec=dec+(d*pow(2,k))
(viii)b=b/10
(ix)k++
Page No. :

(x)end while
(xi)print the decimal number
(xii)break
(xiii)end case
Step 7 : (i)for case 3
(ii)exit(0)
(iii)break
(iv)end case
Step 8: (i)default :
(ii)print "Wrong Choice"
Step 9 : End Switch Case
Step 10: Do you want to continue (Y/N)?? :
Step 11: Input and read the value of c
Step 12: if(c='y' || c='Y')
Step 13: Repeat Step 1
Step 14: End if
Step 15: Stop

Program Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
Page No. :

{
int ch;

//type decleration

char c;

//type decleration

clrscr();

//clears screen

l:printf("\n\n 1:Decimal To Binary ");


printf("\n\n 2:Binary To Decimal ");
printf("\n\n 3:Exit ");
printf("\n\n Enter Choice ");
scanf("%d",&ch);
ch

//reads the value of

switch(ch)
statement

//switch case

{
case 1:
to 1

//works if ch is equal

int de,a[10],i=0;

//type decleration

printf("\n Enter a Decimal no.: ");


scanf("%d",&de);

//reads the value of de

printf("\n The Equivalent binary no. for %d is : ",de);


while(de>0)
{
a[i]=de%2;
dividing the number by 2
i++;
de=de/2;
}

Page No. :

//stores the remainder after


//increments i by 1
//stores the dividend

for(int j=i-1;j>=0;j--)
{
printf("%d",a[j]);
binary number

//prints the equivalent

}
break;
control immediately
case 2:

//terminates the loop


//works if ch is equal to

2
long int k=0,bin,b,dec=0,d;

//type decleration

printf("\n Enter a binary no.");


scanf("%ld",&bin);
b=bin;
number

//reads the value of bin


//copies the binary

while(b>0)
{
d=b%10;

//extracts the digits

dec=dec+(d*pow(2,k));
b=b/10;
k++;

//stores the dividend


//increments k by 1

}
printf("\n The decimal equivalent of %ld is %ld",bin,dec);
break;
control immediately
case 3:
exit(0);
process immediately
Page No. :

//terminates the loop


//works if ch is equal to 3
//terminates the calling

break;
control immediately

//terminates the loop

default:
values does not match

//works if no value is passed or the

printf("\n\n Wrong choice !! ");


}
printf("\n\n Do you want to continue (Y/N)?? :");
fflush(stdin);
values

//clears the garbage

scanf("%c",&c);

//reads the value of c

if(c=='y' || c=='Y')
or not

//checks if c is equal to y

goto l;
//returns the control of the program to the line
specified by label l
getch();

//displays output

Input/Output
Set 1 :
1:Decimal To Binary
2:Binary To Decimal
3:Exit
Enter Choice 1
Enter a Decimal no.: 12
The Equivalent binary no. for 12 is : 1100

Page No. :

Set 2 :
Do you want to continue (Y/N)?? :y
1:Decimal To Binary
2:Binary To Decimal
3:Exit
Enter Choice 2
Enter a binary no.1010
The decimal equivalent of 1010 is 10

Set 3 :
Do you want to continue (Y/N)?? :y
1:Decimal To Binary
2:Binary To Decimal
3:Exit
Enter Choice 4
Wrong choice !!
Do you want to continue (Y/N)?? : n

Discussion
Page No. :

The above program uses switch case technique to convert


decimal numbers into its equivalent binary numbers and vice
versa. The above program runs on call by menu method and
stops only when the user does not want to continue. The program
works for all binary and decimal numbers.

Page No. :

Вам также может понравиться