C program to convert decimal to binary number
In this example, we will learn how to convert decimal to binary number system in a c programming language. Following is the solution to the problem.
Code:
#include <math.h>
#include <stdio.h>
int main() {
int num,original;
printf("Enter a decimal number: ");
scanf("%d", &num);
original=num;
long long binary = 0;
int remainder, i = 1;
while (num != 0) {
remainder = num % 2;
num /= 2;
binary += remainder * i;
i *= 10;
}
printf("%d in decimal = %lld in binary", original, binary);
return 0;
}
Output:
Enter a decimal number: 15
15 in decimal = 1111 in binary
Comments
Post a Comment