- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
Program for Binary to Decimal Number
Binary to Decimal Number
We will write a C program to display the conversion of binary number to decimal number and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the coversion of binary number to decimal number easily by using proper syntax and algorithms.
Working of Program :
In the program, we will require some numbers from the user to display the conversion of binary number to decimal number.
Important Note :
- If the number is expressed by base 10 numeral system then that number is decimal number.
- If the number is expressed by base 2 numeral system then that number is binary number.
Syntax for while() loop:
while(condition) { // Statements // Increment / Decrement }
Problem 1
Write a program to convert the binary number into decimal number using while loop.
- Firstly, we have to enter the number.
- Then print the number.
Code
#include<stdio.h> int main () { int num, binary_num, decimal_num = 0, base = 1, rem; printf ("Enter a binary number = "); scanf (" %d", &num); binary_num = num; while (num > 0) { rem = num % 10; decimal_num = decimal_num + rem * base; num = num / 10; base = base * 2; } printf ("The decimal number = %d", decimal_num); return 0; }
Output
Enter a binary number = 1001 The decimal number = 9
Problem 2
Write a program to convert the binary number into decimal number using function.
- Firstly, we have to enter the number.
- Then print the number.
Code
#include<stdio.h> #include<conio.h> #include<math.h> int binaryTodecimal (int bin_num); int main () { int bin_num, dec_num; printf ("Enter the binary number = "); scanf ("%d", &bin_num); dec_num = binaryTodecimal (bin_num); printf ("Decimal number = %d", dec_num); } int binaryTodecimal (int bin_num) { int decimal_num = 0, temp = 0, rem; while (bin_num != 0) { rem = bin_num % 10; bin_num = bin_num / 10; decimal_num = decimal_num + rem * pow (2, temp); temp++; } return decimal_num; }
Output
Enter the binary number = 101 Decimal number = 5
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment