Home » C program to convert a positive decimal number to binary, octal, and hexadecimal number using recursion

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

Learn about C program to convert a positive decimal number to binary, octal, and hexadecimal number using recursion techniques in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C program to convert a positive decimal number to binary, octal, and hexadecimal number using recursion techniques

Program:

#include<stdio.h>

int convert(int num, int base)
{
   int rem = num % base;

   if(num==0) return;

   convert(num/base, base);

   if(rem<10) printf("%d",rem);
   else printf("%c",rem-10+'A');
}

int main()
{
   int number;

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

   printf("The Binary value of %d = ",number);
   convert(number,2);

   printf("\nThe Octal value of %d = ", number);
   convert(number,8);

   printf("\nThe Hexadecimal value of %d = ", number);
   convert(number,16);

   return 0;
}

Output:

Enter a positive decimal number: 10
The Binary value of 10 = 1010
The Octal value of 10 = 12
The Hexadecimal value of 10 = A

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C program to find the sum of digits in a number using recursion
C program to find the power of a number using a recursive function