We all know how to convert Decimal number to Binary number. This example shows you how to implement that logic into our program. We simply the follow the following steps to get our requirement:
- get reminder by dividing 2
- multiply each reminder by 10 according to its position and add
- divide the number by 2 and get remaining number.
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int dec, temp, bin = 0, i = 1, rem;
printf("Convert Decimal number to Binary Number");
printf("\n------------------------------------------\n");
printf("Enter Number:");
scanf("%d",&dec);
temp = dec;
do
{
rem = temp % 2;
bin += rem * i;
i *= 10;
temp = temp / 2;
}while(temp != 0);
printf("Decimal Value = %d, Binary = %d", dec, bin);
getch();
}
Output:
No comments:
Post a Comment