Ques:-17 Write a program to receive an integer and find its octal, hexadecimal and binary equivalent.

Below is a C program that receives an integer from the user and prints its octal, hexadecimal, and binary equivalents.

				
					#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,binnum,octnum,hexnum,i=0,l=0,m=0,bin[20],oct[20],hex[20];
clrscr();
printf("enter any decimal number:");
scanf("%d",&num);
binnum=num;
octnum=num;
hexnum=num;
while(binnum>0)
{
bin[i]=binnum%2;
binnum=binnum/2;
i++;
}

while(octnum>0)
{
oct[l]=octnum%8;
octnum=octnum/8;
l++;
}

while(hexnum>0)
{
hex[m]=hexnum%16;
hexnum=hexnum/16;
m++;
}

printf("binary value of %d is:",num) ;
for(int j=i-1;j>=0;j--)
{
printf("%d",bin[j]);
}
printf("\n");

printf("octal value of %d is:",num) ;
for(int p=l-1;p>=0;p--)
{
printf("%d",oct[p]);
}
printf("\n");

printf("hexadecimal value of %d is:",num);
for(int q=m-1;q>=0;q--)
{
switch(hex[q])
{
case 10:
	printf("A");
	break;
case 11 :
	printf("B");
	break;
case 12:
	printf("C");
	break;
case 13:
	printf("D");
	break;
case 14:
	printf("E");
	break;
case 15:
	printf("F");
	break;
default :
	printf("%d",hex[q]);
}
//printf("%d",hex[q]);
}
getch();
}

				
			

Out Put

				
					Enter any decimal number; 15
binary value of 15 is : 1111
octal Value of 15 is : 17
hexadecimal value of 15 is :F 
				
			

Another Program

				
					#include <stdio.h>

// Function to convert a number to binary and print it
void printBinary(int num) {
    int binary[32];
    int i = 0;
    
    while (num > 0) {
        binary[i] = num % 2;
        num = num / 2;
        i++;
    }
    
    printf("Binary equivalent: ");
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", binary[j]);
    }
    printf("\n");
}

int main() {
    int num;
    
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Print octal equivalent
    printf("Octal equivalent: %o\n", num);
    
    // Print hexadecimal equivalent
    printf("Hexadecimal equivalent: %X\n", num);
    
    // Print binary equivalent
    if (num == 0) {
        printf("Binary equivalent: 0\n");
    } else {
        printBinary(num);
    }

    return 0;
}

				
			

Explanation:

  1. Binary Conversion Function:

    • printBinary(int num): This function converts the given integer to its binary equivalent.
    • An array binary[32] is used to store binary digits.
    • The loop continues until num becomes zero, storing each bit in the array.
    • After the loop, it prints the binary digits in reverse order.
  2. Main Function:

    • Prompts the user to enter an integer.
    • Uses %o format specifier to print the octal equivalent.
    • Uses %X format specifier to print the hexadecimal equivalent.
    • Calls printBinary function to print the binary equivalent. Special case for zero is handled separately to directly print “0”.

Sample Output:

				
					Enter an integer: 42
Octal equivalent: 52
Hexadecimal equivalent: 2A
Binary equivalent: 101010

				
			

To compile and run this program, save it in a file named NumberConversion.c, compile it using gcc NumberConversion.c -o NumberConversion, and run it using ./NumberConversion. The program will prompt you to enter an integer and then display its octal, hexadecimal, and binary equivalents.

Scroll to Top