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