OverIQ.com

C Program to convert a decimal number to binary, octal and hexadecimal using recursion

Last updated on September 24, 2020


The following is a C program to convert a decimal number to binary, octal and hexadecimal using recursion:

 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**********************************************************
 Program to convert a decimal number to binary, 
 octal and hexadecimal using recursion 
 * 
 * Select conversion: 

 * 1. Decimal to binary. 
 * 2. Decimal to octal. 
 * 3. Decimal to hexadecimal. 
 * 4. Exit. 

 * Enter your choice: 1
 * Enter a number: 4
 * Result = 100
 **********************************************************/

#include<stdio.h> // include stdio.h library
void convert_to_x_base(int, int);

int main(void)
{
    int num, choice, base;

    while(1)
    {
        printf("Select conversion: \n\n");
        printf("1. Decimal to binary. \n");
        printf("2. Decimal to octal. \n");
        printf("3. Decimal to hexadecimal. \n");              
        printf("4. Exit. \n");

        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                base = 2;
                break;
            case 2:
                base = 8;
                break;              
            case 3:
                base = 16;
                break;
            case 4:
                printf("Exiting ...");
                exit(1);
            default:
                printf("Invalid choice.\n\n");
                continue;
        }

        printf("Enter a number: ");
        scanf("%d", &num);

        printf("Result = ");

        convert_to_x_base(num, base);

        printf("\n\n");
    }        

    return 0; // return 0 to operating system
}

void convert_to_x_base(int num, int base)
{    
    int rem;

    // base condition
    if (num == 0)
    {
        return;
    }

    else
    {
        rem = num % base; // get the rightmost digit        
        convert_to_x_base(num/base, base);  // recursive call        
        if(base == 16 && rem >= 10)
        {
            printf("%c", rem+55);
        }

        else
        {
            printf("%d", rem);
        }
    }

}

Try it now

Expected Output:

 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
Select conversion:

1. Decimal to binary. 
2. Decimal to octal. 
3. Decimal to hexadecimal. 
4. Exit.

Enter your choice: 1
Enter a number: 7
Result = 111

Select conversion:

1. Decimal to binary. 
2. Decimal to octal. 
3. Decimal to hexadecimal. 
4. Exit.

Enter your choice: 2
Enter a number: 25
Result = 31

Select conversion:

1. Decimal to binary. 
2. Decimal to octal. 
3. Decimal to hexadecimal. 
4. Exit.

Enter your choice: 4
Exiting ...

How it works #

The following figure shows how the evaluation of convert_to_x_base(4, 2) takes place:

Evaluation of convert_to_xbase(4, 2)


Recommended Reading: