| | |
Convert Decimal to Radix
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> #define SIZE 25 int main(void) { int d, b; printf("Enter decimal and base: "); scanf("%d", &d); scanf("%d", &b); if(b<2){ printf("Your base is too low! \n"); } else if (b >16){ printf("Your base is too high! \n"); } else if(d==0){ printf("%d \n", 0); } else{ while (d!=0){ char temp[SIZE]; char result[SIZE]; int i, r, n ; r = d%b; d = d/b; char basechars[] = "0123456789ABCDEF"; temp[i++] = basechars[r]; result[n++] = temp[--i]; printf("%c", result); } } system("PAUSE"); }
Last edited by Kombat; Sep 17th, 2009 at 6:16 pm.
You're making this more difficult than it needs to be. Look at this code snippet:
C Syntax (Toggle Plain Text)
while (d != 0) { r = d % b; d = d / b; result[len++] = basechars[r]; } for (i = len-1; i >= 0; --i) { printf("%c", result[i]); }
Last edited by yellowSnow; Sep 17th, 2009 at 8:26 pm.
Manic twiddler of bits
![]() |
Similar Threads
- convert Decimal to Hexadecimal system.. (C)
- Convert decimal to binary number? (Assembly)
- how to convert decimal value as string input to its hex equivalent (Assembly)
- Convert decimal to binary number? (Assembly)
- how to convert decimal to binary (C++)
- Convert decimal to binary and with base the 8 (C)
- How to convert a decimal number into its binary equivalent (Python)
Other Threads in the C Forum
- Previous Thread: Previous declaration of ___ was here
- Next Thread: c prgrams
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






