Home / Programs / Program to convert a positive decimal number to Binary, Octal or Hexadecimal using recursion
🚀 Programming Example

Program to convert a positive decimal number to Binary, Octal or Hexadecimal using recursion

👁 1,515 Views
💻 Practical Program
📘 Step Learning
Program to convert a positive decimal number to Binary, Octal or Hexadecimal using recursion

💻 Program Code

#include<stdio.h>
void convert(int, int);
main()
{
int num;
printf("Enter a positive decimal number : ");
scanf("%d", &num);
convert(num, 2);
printf("\n");
convert(num, 8);
printf("\n");
convert(num, 16);
printf("\n");
}/*End of main()*/

void 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' );
}/*End of convert()*/
                        

🖥 Program Output

Enter a positive decimal number : 6
110
6
6
Press any key to continue . . .
                            

📘 Explanation

Program to convert a positive decimal number to Binary, Octal or Hexadecimal using recursion
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.