1#include <math.h>
2#include <stdio.h>
3int convert(long long n);
4int main() {
5 long long n;
6 printf("Enter a binary number: ");
7 scanf("%lld", &n);
8 printf("%lld in binary = %d in decimal", n, convert(n));
9 return 0;
10}
11
12int convert(long long n) {
13 int dec = 0, i = 0, rem;
14 while (n != 0) {
15 rem = n % 10;
16 n /= 10;
17 dec += rem * pow(2, i);
18 ++i;
19 }
20 return dec;
21}
22
1 /*
2 * C Program to Convert Binary to Hexadecimal
3 * My Github: https://github.com/krishnan-tech
4 */
5#include <stdio.h>
6
7int main()
8{
9 long int binaryval, hexadecimalval = 0, i = 1, remainder;
10
11 printf("Enter the binary number: ");
12 scanf("%ld", &binaryval);
13 while (binaryval != 0)
14 {
15 remainder = binaryval % 10;
16 hexadecimalval = hexadecimalval + remainder * i;
17 i = i * 2;
18 binaryval = binaryval / 10;
19 }
20 printf("Equivalent hexadecimal value: %lX", hexadecimalval);
21 return 0;
22}
1#include <stdio.h>
2#include <string.h>
3#include <math.h>
4int binary_converter(char binary[], int length)
5{
6 int decimal = 0;
7 int position = 0;
8 int index = length - 1;
9 while (index >= 0)
10 {
11 decimal = decimal + (binary[index] - 48) * pow(2, position);
12 index--;
13 position++;
14 }
15 return decimal;
16}
17int main()
18{
19 printf("\n\t\t\tBINARY TO DECIMAL CONVERTER VIA TERMINAL\n\n\n");
20 char binary[500];
21 int decimal = 0;
22 int length;
23
24 printf("\t You have to enter a binary number and we will convert into decimal for you. type 'x' to exit\n");
25 while (1)
26 {
27 printf("BINARY : ");
28 scanf("%s", binary);
29 printf("\n");
30 length = strlen(binary);
31 for (int i = 0; i < length; i++)
32 {
33 if (binary[i] == 'x')
34 {
35
36 printf("\nThanks for using our Converter.\n\n");
37 return 0;
38 }
39 if (binary[i] < 48 || binary[i] > 49)
40 {
41 printf("%s is not a BINARY number. \n\n", binary);
42 break;
43 }
44 else
45 {
46 if (i == length - 1)
47 {
48 decimal = binary_converter(binary, length);
49 printf("DECIMAL = %d \n\n", decimal);
50 }
51 continue;
52 }
53 }
54 }
55
56 return 0;
57}