How to print an array of char backward.
- From: hank <ilona-rademaker@xxxxxxxxxxx>
- Date: 30 May 2008 19:34:17 GMT
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;
}
/**********************************************************************/
.
- Follow-Ups:
- Re: How to print an array of char backward.
- From: Tomás Ó hÉilidhe
- Re: How to print an array of char backward.
- From: santosh
- Re: How to print an array of char backward.
- Prev by Date: Re: c compilation - gcc vs visual c
- Next by Date: Re: reporting typedef errors automatically
- Previous by thread: Help with atoi function for a numero program.
- Next by thread: Re: How to print an array of char backward.
- Index(es):
Relevant Pages
|