mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 21:41:59 +03:00
39 lines
752 B
C
39 lines
752 B
C
|
/*********decimal number to binary number conversion*****************/
|
||
|
#include <stdio.h>
|
||
|
void decimal2Binary(long num);
|
||
|
|
||
|
int main(){
|
||
|
|
||
|
long num;
|
||
|
|
||
|
printf("Enter a decimal integer \n");
|
||
|
scanf("%ld", &num);
|
||
|
decimal2Binary(num);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/***function for convert decimal numbers to binary numbers****************/
|
||
|
void decimal2Binary(long num){
|
||
|
|
||
|
long decimal_num, remainder, base, binary, no_of_1s;
|
||
|
|
||
|
base = 1;
|
||
|
binary = 0;
|
||
|
no_of_1s = 0;
|
||
|
|
||
|
while (num > 0)
|
||
|
{
|
||
|
remainder = num % 2;
|
||
|
|
||
|
if (remainder == 1)
|
||
|
{
|
||
|
no_of_1s++;
|
||
|
}
|
||
|
binary = binary + remainder * base;
|
||
|
num = num / 2;
|
||
|
base = base * 10;}
|
||
|
|
||
|
printf("Its binary equivalent is = %ld\n", binary);
|
||
|
}
|