Program that turns decimal base to any base from 2-16 gives reverse number

Hello!
I am trying to make a program that turns any number with decimal base in number with any base from 2-16, but the program I have written gives me the number in reverse order. I tried to combine my program with a regular program that give a reverse number for number that has been entered, but then my program just returns 0000 all the time. I have no idea how I should do it, so I would be thankful for any help! Here is what I have done - it returns the reverse of what is needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <iostream>
using namespace std;

int main(void)
{
    int m, n, c;
    printf("Enter two integers: ");
    scanf("%d", &m);
    scanf("%d", &n);
    printf("%d\n", m);
    printf("%d\n", n);
    printf(" \n");
    if(n < 2 || n > 16)
    {
        printf("You have entered invalid base value.\n");
            return 0;
    }
    else if(n > 1 && n < 17)
    {
        while(m != 0)
        {
            c = (m%n);
            m = (m/n);
            if( c == 10)
            {
                c = printf("A");
            }
            else if( c == 11)
            {
                c = printf("B");
            }
            else if( c == 12)
            {
                c = printf("C");
            }
            else if( c == 13)
            {
                c = printf("D");
            }
            else if( c == 14)
            {
                c = printf("E");
            }
            else if( c == 15)
            {
                c = printf("F");
            }
            else
            {
                printf("%d", c);
            }
        }
        printf("\n");
    };
}
Last edited on
You could store the ASCII digits in a string and then reverse the string. Another way is to just use a character array of sufficient size and store the ASCII digits starting at the back and working your way forwards.

You can convert the binary digit to ASCII using a trick like this:
1
2
3
4
5
const char table[] = "0123456789ABCDEF";
char ch;
...
c = m % n;
ch = table[c];

Topic archived. No new replies allowed.