| | |
Convert Decimal to Radix
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
You are required to write a C program that accepts two decimal integers, say d and r. You
may assume that the first decimal integer d is nonnegative and that the second decimal integer r
which represents the radix, will be one of 2, 3, 4, . . ., 15, 16. The program should convert the first
decimal integer d into its representation in radix r and print the result.
I seriously am clueless on how to start. What I want to do is take a decimal divide by a base record the remainder and keep doing so with the decimal until the decimal is 0 and record the remainders. That is how I get the binary.
This is what I got so far its absolutely nothing
may assume that the first decimal integer d is nonnegative and that the second decimal integer r
which represents the radix, will be one of 2, 3, 4, . . ., 15, 16. The program should convert the first
decimal integer d into its representation in radix r and print the result.
I seriously am clueless on how to start. What I want to do is take a decimal divide by a base record the remainder and keep doing so with the decimal until the decimal is 0 and record the remainders. That is how I get the binary.
This is what I got so far its absolutely nothing
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void){ int d, r, temp; temp = d = r = 0; temp = d/r;
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
fixed it to this
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void){ int d, r, temp; d = r = 0; for(temp = d; temp !=0; temp%r); }
Last edited by Kombat; Sep 17th, 2009 at 1:41 am.
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
I've been working on this for a while and got this code. It is a pretty good start. But not sure where to go from here
I do not want it to print in reverse
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int d, b, c; printf("please enter decimal and base"); scanf("%d\n", &d); scanf("%d", &b); while(d!=0){ int r; r = d%b; d = d/b; printf("\n%d", c); } }
I do not want it to print in reverse
Last edited by Kombat; Sep 17th, 2009 at 1:17 pm.
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int d, b, c; printf("Enter decimal and base: "); scanf("%d", &d); scanf("%d", &b); if(b<2){ printf("Your base is too low! \n"); } else{ while(d!=0){ int r; r = d%b; d = d/b; char basechars[] = "0123456789ABCDEF"; printf("%c", basechars[r]); } }
Thanks, however it is still printing the result in reverse:
Example: 25 3 it prints 122 when answer should be 221
Last edited by Kombat; Sep 17th, 2009 at 1:57 pm.
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int d, b, c; printf("Enter decimal and base: "); scanf("%d", &d); scanf("%d", &b); if(b<2){ printf("Your base is too low! \n"); } else{ while(d!=0){ int r; r = d%b; d = d/b; char basechars[] = "0123456789ABCDEF"; printf("%c", basechars[r]); } } system("PAUSE"); }
I have the char's stored in an array however the conversion is being printed out backwards.
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
I am not sure how to do that. I tried this
but it doesn't work
C Syntax (Toggle Plain Text)
int r; char i; char result[ARRAY_SIZE]; r = d%b; d = d/b; char basechars[] = "0123456789ABCDEF"; basechars[r] = i; printf("%c", result[i]);
but it doesn't work
![]() |
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 |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






