In this tutorial you will learn about the C Program to Convert Binary to Hexadecimal and its application with practical example.
C Program to Convert Binary to Hexadecimal
In this tutorial, we will learn to create a C program that will Convert Binary to Hexadecimal in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
Program to Convert Binary to Hexadecimal:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We cal perform many input-output operations using c programming. In today’s tutorial, we take the input in Binary from the user and convert it into HexaDecimal. With the help of c programming, we can perform many conversion operations.
With the help of this program, we can Convert Binary to Hexadecimal.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Takeing the input number from the user in binary for the program. 3. Passing that input to the string function. 4. Pass that number to a for loop for convertion. 4. Print the Result. 5. End the program. |
Program to Convert Binary to Hexadecimal:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/* C Program to Convert Binary to Hexadecimal */ #include<stdio.h> void main() { //declaring the variable for the program long int binary_number, hexadecimal_number = 0, i = 1, remainder; /* taking input from the user in binary number */ printf("Please Enter any Binary Number: "); //scanning the input number scanf("%ld", &binary_number); //passing that number to the while loop for convertion while (binary_number != 0) { remainder = binary_number % 10; hexadecimal_number = hexadecimal_number + remainder * i; i = i * 2; binary_number = binary_number / 10; } //printing the output number in hexadecimal printf("Equivalent Hexadecimal Number %lX", hexadecimal_number); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- binary_number = in will hold the integer value.
Taking the input from the message.