Home  >  Article  >  Backend Development  >  How to convert binary to decimal in C language

How to convert binary to decimal in C language

藏色散人
藏色散人Original
2020-03-03 10:57:1928353browse

How to convert binary to decimal in C language

How to convert binary in C language to decimal?

Follow the following steps to use C language to convert binary numbers into decimal numbers:

Recommended: "C Language Tutorial"

1. First, set the function Sum in the main function, and define an array array[8] to store the input eight-digit binary number.

How to convert binary to decimal in C language

#2. Then use a for loop statement to input an eight-digit binary number. In the scanf function, add a 1 between %d, then use the printf function to output, and call the Sum function with the array name as an actual parameter.

How to convert binary to decimal in C language

#3. In the Sum function, a power function is declared, where n is the exponent, and term is the number converted to decimal for each digit, such as 1 *(2*2), sum is the total.

How to convert binary to decimal in C language

4. Then use a for loop statement, i is used to control the number of loops, n starts from the highest index 7 and decreases by one each time it loops, if the index is greater than or equal to zero , perform a loop, use n as an actual parameter when calling the power function, and finally return the sum.

How to convert binary to decimal in C language

#5. In the power function, if b is equal to zero, that is, the index is zero, then 1 is returned, otherwise it enters a loop, and j is used to control the number of loops.

How to convert binary to decimal in C language

#6. Finally, look at the running results. This C language program can successfully convert binary numbers into decimal numbers.

How to convert binary to decimal in C language

For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of How to convert binary to decimal in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn