H
hank
I have this code here, it converts decimal numbers to binary. There is
one problem. The output is printed 'in reverse' and I have no clue at all
how to solve this problem.
Output of program:
Decimal number: 10
Binary representation: 0101
(It should be 1010, and not 0101...)
I would be glad if someone could help me. Maybe there is some function,
which does the trick... (I have no idea.)
Thanks.
/**********************************************************************/
#include <stdio.h>
#include <string.h>
int main()
{
char bin[25] = "";
int dec;
printf("Decimal number: ");
scanf("%d", &dec);
do {
if (dec % 2 == 0)
strcat(bin, "0");
else {
strcat(bin, "1");
dec--;
}
dec = dec / 2;
} while (dec > 0);
printf("Binary representation: %s\n", bin);
return 0;
}
/**********************************************************************/
one problem. The output is printed 'in reverse' and I have no clue at all
how to solve this problem.
Output of program:
Decimal number: 10
Binary representation: 0101
(It should be 1010, and not 0101...)
I would be glad if someone could help me. Maybe there is some function,
which does the trick... (I have no idea.)
Thanks.
/**********************************************************************/
#include <stdio.h>
#include <string.h>
int main()
{
char bin[25] = "";
int dec;
printf("Decimal number: ");
scanf("%d", &dec);
do {
if (dec % 2 == 0)
strcat(bin, "0");
else {
strcat(bin, "1");
dec--;
}
dec = dec / 2;
} while (dec > 0);
printf("Binary representation: %s\n", bin);
return 0;
}
/**********************************************************************/