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

#include <stdio.h>

int main(void){
    
    int d, r, temp;
    temp = d = r = 0;
    temp = d/r;

fixed it to this

#include <stdio.h>
 int main(void){
    
    int d, r, temp;
    d = r = 0;
    

    for(temp = d; temp !=0; temp%r);
}

> That is how I get the binary.
That's how you do it for every other base as well.

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

#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

Well it's good for bases < 10 char basechars[] = "0123456789"; and printf("%c", basechars[r] );

#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

#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.

I am not sure how to do that. I tried this

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

result[numchars++] = basechars[r];

When you're done, print from numchars-1 back down to 0 (one char at a time).

when I add the result[i++] the program crashes, I think because it keeps incrementing infinity

Post your code!
Did you initialise i ?

commented: For utter utter patience! +7
commented: very very patient and helpful!! +1
commented: You're a patient man Salem. And the help you provide is excellent! +23
#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");

                        }

You're making this more difficult than it needs to be. Look at this code snippet:

while (d != 0) {
        r = d % b;
        d = d / b;
        result[len++] = basechars[r];
    }

    for (i = len-1; i >= 0; --i) {
        printf("%c", result[i]);
    }

Wow thanks it works however it is showing a lot of weird symbols along with the answer.

Wow thanks it works however it is showing a lot of weird symbols along with the answer.

Post your full code.
And don't thank me - Salem is the one you should be thanking.

I pmed you it

Initialize k to zero.
Also, move your declarations for r and basechars outside of the loop.

commented: Very Helpful +1

the moving r and basechars made it fail but initializing the k to zero did the trick.

Thanks very much to you and many thanks to salem :)

the moving r and basechars made it fail but initializing the k to zero did the trick.

Thanks very much to you and many thanks to salem :)

Not sure why moving those declarations outside the loop would make it fail - it works for me:

#include <stdio.h>

#define SIZE 64

int main(void) {
    int d, b, k=0, i, r;
    char result[SIZE];
    char basechars[] = "0123456789ABCDEF";

    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) {
            r = d % b;
            d = d / b;
            result[k++] = basechars[r];
        }
        for (i = k - 1; i >= 0; --i) {
            printf("%c", result[i]);
        }

    }
    return 0;
}

BTW - if you're happy with the outcome - flag the post as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.