- c program to convert binary to decimal using array and for loop.
- Lets us see an example program on c to convert binary format number to decimal format.
- Write a function which accepts a number as binary.
- rem = n%10;
- n /= 10;
- decimal += rem*pow(2,i);
- This is the logic to get decimal format of the number.
Program #1 : write a c program to convert binary to decimal using while loop function
- #include <stdio.h>
- int convertBinaryToDecimal(long long n);
- int main()
- {
- long long n;
- printf("Enter a binary number: ");
- scanf("%lld", &n);
- printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
- getch();
- }
- int convertBinaryToDecimal(long long n)
- {
- int decimal = 0, i = 0, rem;
- while (n!=0)
- {
- rem = n%10;
- n /= 10;
- decimal += rem*pow(2,i);
- ++i;
- }
- return decimal;
- }
Output:
Program #2 : write a c program to convert binary to decimal using while loop function
- #include <stdio.h>
- int convertBinaryToDecimal(long long n);
- int main()
- {
- long long n;
- printf("Enter a binary number: ");
- scanf("%lld", &n);
- printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
- getch();
- }
- int convertBinaryToDecimal(long long n)
- {
- int decimal = 0, i = 0, rem;
- for (i=0; n!=0;i++)
- {
- rem = n%10;
- n /= 10;
- decimal += rem*pow(2,i);
- }
- return decimal;
- }
Output:
- Enter a binary number:
- 1011
- 1011 binary format= 11 decimal format
You Might Like:
- Write a c program to generate fibonacci series without recursion
- Write a c program to generate fibonacci series using recursion
- Matrix multiplication in c program with explanation
- Factorial program in c without recursion
- C program to check number is armstrong number or not
- Check Armstrong number in c using recursive function
- C program for even or odd using for loop
- C program to check odd or even without using modulus operator and division operator
- Prime number program in c using for loop and while loop
- C program to print prime numbers from 1 to n
- C program to print multiplication table using while loop and for loop
- C program to convert binary to decimal using for loop
- C program to reverse a number using while loop and for loop
- C program to find sum of n numbers using for loop
- C program to find leap year using if else
- c program to check leap year using conditional operator
- C program to print patterns of numbers,alphabets and stars in a pyramid shape
- C program to swap two numbers without using third variable and using functions
- C Program to swap two numbers without using third variable
- Write a C program to swap two integer arrays
- C program for swapping of two strings
- C program to print given number in words
- C program to print 1 to 100 without using loop
- C program to find ASCII value of a string
- Convert string to integer c programming
- C program to display characters from a to z using loop
- C programming power function example program
- C program for addition subtraction multiplication and division using switch case
- C program to delete an element in an array
- C program to insert an element in an array
- Switch case in c example program
- Prime number program in c using for loop
- Fibonacci series in c without recursion
No comments