Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Binary2Decimal.c #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Binary2Decimal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <conio.h>
void main()
{
// declaration of variables
int num, binary_num, decimal_num = 0, base = 1, rem;
printf (" Enter a binary number with the combination of 0s and 1s \n");
scanf (" %d", &num); // accept the binary number (0s and 1s)

binary_num = num; // assign the binary number to the binary_num variable


while ( num > 0)
{
rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */
decimal_num = decimal_num + rem * base;
num = num / 10; // divide the number with quotient
base = base * 2;
}

printf ( " The binary number is %d \t", binary_num); // print the binary number
printf (" \n The decimal number is %d \t", decimal_num); // print the decimal
getch();
}