In this tutorial you will learn about the C Program to Convert Binary to Decimal and its application with practical example.
C Program to Convert Binary to Decimal
In this tutorial, we will learn to create a C program that will Convert Binary to Decimal 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.
Program to Convert Binary to Decimal:-
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 Decimal. With the help of c programming, we can perform many conversion operations.
With the help of this program, we can Convert Binary to Decimal.
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 Decimal:-
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 29 30 31 32 33 34 |
/* C Program to Convert Binary to Decimal */ #include <stdio.h> #include <math.h> //user defined function for convertion declaration int convert(long long); //main function for the program int main() { //declarint the variable long long n; //taking input for the conertion printf("Enter a binary number: "); scanf("%lld", &n); //printing the outout after convertion printf("%lld in binary = %d in decimal", n, convert(n)); return 0; } // function definition int convert(long long n) { int dec = 0, i = 0, rem; while (n!=0) { rem = n % 10; n /= 10; dec += rem * pow(2, i); ++i; } return dec; } |
Output:-
In the above program, we have first initialized the required variable.
- n = in will hold the long integer value for the input.
Taking the input.