Experiment 9
Experiment 9
OBJECTIVE:-
INTRODUCTION:-
In this experiment, through the creation of a basic program, we will try to understand about
while looping construct, the switch case construct and about the basic features of C language,
i.e., it’s syntax, structure and compilation process.
ALGORITHM:-
PROGRAM CODE:-
#include<stdio.h>
#include<math.h>
void main()
{
printf("Name: gaurav ahirwar Roll No.: 24/B01/062\n");
int j=0,d=0,num,i=0,b=0,n;
char c;
printf("Choose D for Decimal to Binary conversion.\n");
printf("Choose B for Binary to Decimal conversion.\n");
scanf("%c",&c);
switch(c)
{
case 'D':
printf("Enter a decimal number.\n");
scanf("%d",&n);
while(n>0)
{
b+=(n%2)*pow(10,i);
n=n/2;
i++;
}
printf("Binary equivalent = %d",b);
break;
case 'B':
printf("Enter a binary number.\n");
scanf("%d",&num);
while(num>0)
{
d+=(num%10)*pow(2,j);
num=num/10;
j++;
}
printf("Decimal equivalent = %d",d);
break;
default: printf("Wrong input");
}
}
OUTPUT:-
Upon successful compilation and execution, the program will produce the following output:
RESULT:-
The binary equivalent, if the number input is decimal and decimal equivalent, if the number
input is binary, based on the user’s choice, is successfully displayed on the console, upon the
successful compilation and execution of the program.
This experiment gave us an insight into the basic structure of a C program. We learned the
following: