Skip to content

Instantly share code, notes, and snippets.

@DeclanGas
Created June 23, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeclanGas/92d7bd6a2b5306c39726ccd182b67620 to your computer and use it in GitHub Desktop.
Save DeclanGas/92d7bd6a2b5306c39726ccd182b67620 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#define BASE 2
// returns true if binary, false if not binary
bool check_binary(int b)
{
while (b > 0)
{
if (b % 10 > 1)
{
return false;
}
b = b / 10;
}
return true;
}
// calculates decimal equivalent of binary number
int bin_to_dec(int b)
{
// declare a variable to hold decimal value
int d = 0;
int a = 0;
// iterate through each binary digit
while (b > 0)
{
d += b % 10 * pow(BASE, a);
b = b / 10;
a++;
}
// when loop finishes return decimal value
return d;
}
int main(void)
{
int binary = get_int("Enter Binary Number: ");
if (check_binary(binary) == false)
{
printf("Invalid input. Try again!\n");
return 1;
}
int decimal = bin_to_dec(binary);
printf("%i in binary is %i in decimal\n", binary, decimal);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment