README.md

December 4, 2022 ยท View on GitHub

A bin2dec converter

Description

A binary to decimal converter

More Info

A binary number

the number in decimal

Submitted On
Bykoby-GR
LevelBeginner
User Rating4.0 (8 globes from 2 users)
CompatibilityC
CategoryMath
WorldC / C++
Archive File

Source Code

/* Binary to decimal converter
 *
 *        by : koby
 *           koby@in.gr
 * If you like this code, visit my site
 *           http://www.codecraft.tk
 */
#include <stdio.h>
#include <math.h>
int main(void) {
	int dec=0, flag=0.0;
	int bin, bit;
	double exp=0.0;
	printf("Enter a binary number : ");
	scanf("%d", &bin);
	while(bin) {
		bit=bin%10;
		if(bit!=0 && bit!=1) {
			flag=1;
		}
		bin=bin/10;
		dec=dec+bit*pow(2, exp);
		exp++;
	}
	if(flag) {printf("\n+++ Not a binary number !!!\n");}
	else {printf("\n+++ Number in decimal : %d\n", dec);}
	return 0;
}