This program converts decimal to binary numbers. It works for positive integers 0-127. To see how conversions between number systems work, read the post on The Decimal, Binary, Octal & Hexadecimal Number Systems
Executable – Download decimal2binary.exe
Code –
#include<stdio.h> #include<conio.h> /* program to convert decimal to binary - works for positive numbers 0-127 */ int main() { int x, i=0; printf("Enter the decimal number\n"); scanf("%d", &x); int y=x; int j=0; /* this loop gets a count of the number of bigits/remainders */ while(x!=0) { i=x%2; x=x/2; j++; } x=y; int n=0, a[n]; /* second loop feeds values of bigits to array */ while(n<j && x!=0) { i=x%2; x=x/2; a[n]=i; n++; } n=0; printf("The binary equivalent is "); /* third loop prints the reversed array */ while(n<j) { printf("%d", a[j-(n+1)]); n++; } getch(); return 0; }
1 Comment
Keep up the knowledge as it gives life