0% found this document useful (0 votes)
9 views3 pages

Experiment 9

The document outlines an experiment to create a C program that converts decimal numbers to binary and vice-versa. It includes an algorithm detailing the steps for input, processing, and output, as well as the program code itself. The experiment emphasizes learning C programming concepts such as loops, switch-case constructs, and basic program structure.

Uploaded by

Gaurav Ahirwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Experiment 9

The document outlines an experiment to create a C program that converts decimal numbers to binary and vice-versa. It includes an algorithm detailing the steps for input, processing, and output, as well as the program code itself. The experiment emphasizes learning C programming concepts such as loops, switch-case constructs, and basic program structure.

Uploaded by

Gaurav Ahirwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT 9

OBJECTIVE:-

Write a program to convert a decimal number to binary and vice-versa.

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: To convert a decimal number to binary and vice-versa.


Input: The choice, c and the number, n or num.
Output: The binary equivalent, if the number entered is a decimal number and vice-versa.
STEP 1: Start the program.
STEP 2: Include the necessary header file: #include<stdio.h> and #include<math.h>.
STEP 3: Define the main function.
STEP 4: Declare variables, j, d, num, i, b, n.
STEP 5: Assign j=0, d=0, i=0, b=0.
STEP 6: switch(c)
If c=’D’, then enter a decimal number, n.
while(n>0)
{ b=b+(n%2)*pow(10,i), n=n/2 and i=i+1;}
Now, print b and exit the case using break.
If c=’B’, then enter a binary number, num.
while(num>0)
{ d=d+(num%10)*pow(2,j), num=num/10 and j=j+1;}
Now, print d and exit the case using break.
For any other choice, print “Wrong Input”.
STEP 7: Stop.

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.

LEARNING FROM EXPERIMENT:-

This experiment gave us an insight into the basic structure of a C program. We learned the
following:

• Creation of a header file.


• Creation of a main function, which is the starting point of the program.
• Usage of printf function, which displays the output on the console.
• Usage of while loop for creating a looping construct in the program, which executes a
set of statements till the condition is satisfied.
• Usage of switch-case construct for selective execution of instructions in a program.
• Usage of break statement to exit the switch-case construct or loop in a program.
• About the successful compilation and execution of the program.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy