» » » C program to convert binary to decimal using for loop

  • 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

  1. #include <stdio.h>
  2. int convertBinaryToDecimal(long long n);
  3.  
  4. int main()
  5. {
  6.     long long n;
  7.     printf("Enter a binary number: ");
  8.     scanf("%lld", &n);
  9.     printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
  10.     
  11.     getch();
  12. }
  13.  
  14. int convertBinaryToDecimal(long long n)
  15. {
  16.     int decimal = 0, i = 0, rem;
  17.     while (n!=0)
  18.     {
  19.         rem = n%10;
  20.         n /= 10;
  21.         decimal += rem*pow(2,i);
  22.         ++i;
  23.     }
  24.     return decimal;
  25. }

 Output:


Binary to decimal in c program


Program #2 : write a c program to convert binary to decimal using while loop function

  1. #include <stdio.h>
  2. int convertBinaryToDecimal(long long n);
  3.  
  4. int main()
  5. {
  6.     long long n;
  7.     printf("Enter a binary number: ");
  8.     scanf("%lld", &n);
  9.     printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
  10.     
  11.     getch();
  12. }
  13.  
  14. int convertBinaryToDecimal(long long n)
  15. {
  16.     int decimal = 0, i = 0, rem;
  17.     for (i=0; n!=0;i++)
  18.     {
  19.         rem = n%10;
  20.         n /= 10;
  21.         decimal += rem*pow(2,i);
  22.       
  23.     }
  24.     return decimal;
  25. }

Output:

  1. Enter a binary number:
  2. 1011
  3. 1011 binary format= 11 decimal format

You Might Like:
  1. Write a c program to generate fibonacci series without recursion  
  2. Write a c program to generate fibonacci series using recursion
  3. Matrix multiplication in c program with explanation
  4. Factorial program in c without recursion
  5. C program to check number is armstrong number or not
  6. Check Armstrong number in c using recursive function  
  7. C program for even or odd using for loop 
  8. C program to check odd or even without using modulus operator and division operator
  9. Prime number program in c using for loop and while loop 
  10. C program to print prime numbers from 1 to n
  11. C program to print multiplication table using while loop and for loop
  12. C program to convert binary to decimal using for loop 
  13. C program to reverse a number using while loop and for loop
  14. C program to find sum of n numbers using for loop 
  15. C program to find leap year using if else  
  16. c program to check leap year using conditional operator  
  17. C program to print patterns of numbers,alphabets and stars in a pyramid shape
  18. C program to swap two numbers without using third variable and using functions 
  19. C Program to swap two numbers without using third variable 
  20. Write a C program to swap two integer arrays  
  21. C program for swapping of two strings  
  22. C program to print given number in words 
  23. C program to print 1 to 100 without using loop
  24. C program to find ASCII value of a string  
  25. Convert string to integer c programming  
  26. C program to display characters from a to z using loop 
  27. C programming power function example program 
  28. C program for addition subtraction multiplication and division using switch case  
  29. C program to delete an element in an array
  30. C program to insert an element in an array
  31. Switch case in c example program
  32. Prime number program in c using for loop
  33. Fibonacci series in c without recursion

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu