Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
24. Write a program to convert a binary number into a decimal number.
/*
File: Prgrm24.c
Author: Aditya Saini
Date: Jan 17, 2021
Description: Program to convert a binary number into a decimal number.
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
int bin_num;
int dec_num;
int temp;
int i;
//Input binary number
printf ("Input binary number: ");
scanf ("%d", &bin_num);
//Convert binary to decimal
dec_num = 0;
temp = bin_num;
i = 0;
while (temp != 0)
{
dec_num = dec_num + (temp % 10) * pow (2, i);
temp = temp / 10;
i++;
}
//Print result
printf ("Decimal conversion of %d: %d", bin_num, dec_num);
};
Output
Input binary number: 10110
Decimal conversion of 10110: 22
No comments:
Post a Comment
Please do not post spam links.