A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to convert a binary number to a hexadecimal number
Q:

C program to convert a binary number to a hexadecimal number

0

C program to convert a binary number to a hexadecimal number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, we will read a number in binary format (0s and 1s) from the user and convert it to a hexadecimal number, and print it on the console screen.

Program:

The source code to convert a binary number to a hexadecimal number is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to convert a binary number to hexadecimal number

#include <stdio.h>

int main()
{
    int binaryNumber = 0;
    int hexNumber = 0;
    int i = 1;
    int rem = 0;

    printf("Enter binary number: ");
    scanf("%d", &binaryNumber);

    while (binaryNumber != 0) {
        rem = binaryNumber % 10;
        hexNumber = hexNumber + rem * i;

        i = i * 2;
        binaryNumber = binaryNumber / 10;
    }

    printf("Hexadecimal Number: %X", hexNumber);
    return 0;
}

Output:

Enter binary number: 1101
Hexadecimal Number: D

Explanation:

Here, we created 4 integer variables binaryNumberhexNumberirem that are initialized with 0. Then we read a number in binary format (1s and 0s). Then we converted the number into a decimal number and printed the corresponding hexadecimal number using the "%X" format specifier on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now