instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
instanceof java is a java related one.
C program to convert binary to decimal using for loop
Posted: May 14, 2017 10:53 AM
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: C program to convert binary to decimal using for loop
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java
Advertisement
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
Read: C program to convert binary to decimal using for loop