Search
 
SCRIPT & CODE EXAMPLE
 

C

binary to decimal in c

#include <stdio.h>
#include <string.h>
#include <math.h>
int binary_converter(char binary[], int length)
{
	int decimal = 0;
	int position = 0;
	int index = length - 1;
	while (index >= 0)
	{
		decimal = decimal + (binary[index] - 48) * pow(2, position);
		index--;
		position++;
	}
	return decimal;
}
int main()
{
	printf("
			BINARY TO DECIMAL CONVERTER VIA TERMINAL


");
	char binary[500];
	int decimal = 0;
	int length;

	printf("	 You have to enter a binary number and we will convert into decimal for you. type 'x' to exit
");
	while (1)
	{
		printf("BINARY : ");
		scanf("%s", binary);
		printf("
");
		length = strlen(binary);
		for (int i = 0; i < length; i++)
		{
			if (binary[i] == 'x')
			{

				printf("
Thanks for using our Converter.

");
				return 0;
			}
			if (binary[i] < 48 || binary[i] > 49)
			{
				printf("%s is not a BINARY number. 

", binary);
				break;
			}
			else
			{
				if (i == length - 1)
				{
					decimal = binary_converter(binary, length);
					printf("DECIMAL = %d 

", decimal);
				}
				continue;
			}
		}
	}

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: c recursion func revers number 
C :: get float in c 
C :: geom boxplot remove outliers 
C :: fgets remove newline 
C :: replacing a character in string in C 
C :: C first digit of integer 
C :: equal string c 
C :: write a c program to find size of variable 
C :: how to add 1 to 10 in c 
C :: c loop 
C :: c check if character is an alphabet 
C :: c convert string to size_t 
C :: pointer arithmetic in c 
C :: declare string in c 
C :: malloc c 
C :: Complete the function in the editor. It has one parameter: an array, . It must iterate through the array performing one of the following actions on each element: 
C :: cifras de un numero en c 
C :: c memcpy array 
C :: C program to find power of any number 
C :: enum case statement in c 
C :: how to debug a segmentation fault in c 
C :: pointer in c 
C :: what is O(N^2) in bubble sort method 
C :: how to declare an array of n numbers in c 
C :: convert c to phyton 
C :: leer string en c 
C :: uninstall elgg from hostgtor 
C :: pointeur de pointeur en language c 
C :: My name is c 
C :: denomination counter 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =