C
Programming and Technical
Programming
Program
write a program to convert decimal to binary.
Read Solution (Total 3)
-
- #include
void main()
{
long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;
printf("Enter a decimal integer n");
scanf("%ld", &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
/* To count no.of 1s */
if (remainder == 1)
{
no_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %dn", decimal_num);
printf("Its binary equivalent is = %ldn", binary);
printf("No.of 1's in the binary number is = %dn", no_of_1s);
} - 6 years agoHelpfull: Yes(1) No(1)
- #include
int main()
{
int n=5,base=1,binary=0,rem;
while(n>0){
rem=n%2;
n/=2;
binary=binary+rem*base;
base=base*10;
}
printf("%d",binary);
} - 5 years agoHelpfull: Yes(0) No(0)
- #include
int main()
{
int n=5,base=1,binary=0,rem;
while(n>0){
rem=n%2;
n/=2;
binary=binary+rem*base;
base=base*10;
}
printf("%d",binary);
} - 5 years agoHelpfull: Yes(0) No(0)
C Other Question