C Program to convert decimal number to Binary, Octal or Hexadecimal

By | March 22, 2017

C Program to convert decimal number to Binary, Octal or Hexadecimal


Write a C Program to convert positive decimal number to Binary, Octal or Hexadecimal. Here’s a Simple Program to convert decimal number to Binary, Octal or Hexadecimal number in C Programming Language.

Create a program that would convert a decimal number to binary, octal or hexadecimal counterpart. Your program should ask the user for decimal with a data type of a long integer.


Decimal system : :


  • Decimal system is the most widely used number system. But computer only understands binary. Binary, octal and hexadecimal number systems are closely related and we may require to convert decimal into these systems.
  • Decimal system is base 10 (ten symbols, 0-9, are used to represent a number) and similarly, binary is base 2, octal is base 8 and hexadecimal is base 16.

Below is the source code for C Program to convert positive decimal number to Binary, Octal or Hexadecimal which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Program to convert a positive decimal number to Binary, Octal or Hexadecimal */


#include<stdio.h>
void convert(int, int);

int main()
{
	int num;
	printf("Enter a positive decimal number : ");
	scanf("%d", &num);
	printf("\nBinary number :: ");
	convert(num, 2);
	printf("\n");
	printf("\nOctal number :: ");
	convert(num, 8);
	printf("\n");
	printf("\nHexadecimal number :: ");
	convert(num, 16);
	printf("\n");

	return 0;
}/*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()*/



OUTPUT  : :


//----------------------OUTPUT ::---------------------------------


//----------------------FIRST RUN ::------------------------------

Enter a positive decimal number : 15

Binary number :: 1111

Octal number :: 17

Hexadecimal number :: F


//-----------------------SECOND RUN ::----------------------------

Enter a positive decimal number : 157

Binary number :: 10011101

Octal number :: 235

Hexadecimal number :: 9D



If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

Leave a Reply