Engineering

Computer ScienceQ&A LibraryI'm trying to put c code together to convert up to a 5 digit binary to decimal . I keep getting wrong answers : here's my c code so far . Can you please explain throughly as I am a begginer . #include <stdio.h> int main() { int a1, b1, c1, d1, e1, n1, decimal; printf( "Enter a five-digit integer in 1's and 0's\n" ); scanf("%d", &n1 ); a1= n1 % 10; b1= n1 / 10 % 10; c1= n1 / 100 % 10; d1= n1 / 1000 % 10; e1= n1 / 10000; decimal = a1 + b1 + c1 + d1 + e1; printf("\nThe decimal equivalent of given binary number %d is: %d\n", n1,decimal); return 0; }Question

Asked Jan 18, 2020

26 views

I'm trying to put c code together to convert up to a 5 digit binary to decimal . I keep getting wrong answers : here's my c code so far . Can you please explain throughly as I am a begginer .

#include <stdio.h>

int main()

{

int a1, b1, c1, d1, e1, n1, decimal;

printf( "Enter a five-digit integer in 1's and 0's\n" );

scanf("%d", &n1 );

a1= n1 % 10;

b1= n1 / 10 % 10;

c1= n1 / 100 % 10;

d1= n1 / 1000 % 10;

e1= n1 / 10000;

decimal = a1 + b1 + c1 + d1 + e1;

printf("\nThe decimal equivalent of given binary number %d is: %d\n", n1,decimal);

return 0;

}

Step 1

**Programming instruction:**

To obtain the decimal equivalent of a given binary number, extract the digits of binary number beginning from the rightmost digits and keep a variable **DecValue**. While extracting the digits from binary number, multiply the digit with the base (power of 2) and add it to the **DecValue** variable. In the end, the **DecValue** will store the required decimal value.

Step 2

**Program:**

#include <math.h>

#include <stdio.h>

int DecimalToBinary(long long n);

int main() {

long long n;

printf("Enter a binary number: "); //Enter binary number

scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, DecimalToBinary(n));

return 0;

}

int DecimalToBinary(long long n) { //Function to covert the binary number into decimal

&...

Tagged in

Find answers to questions asked by student like you

Show more Q&A

Q: What is pseudo code?

A: Pseudocode is used in algorithm and programming based fields to outlines or represents the implement...

Q: Write the pseudocode for a problem that will accept the number of (not value of) quarters, dimes, ni...

A: Pseudo Code: It is used to describe the step by step details of a program.It is used to explain the ...

Q: Optimize the following Boolean expressions using a map: XZ +YZ + XY Z a.

A: Truth Table:

Q: Online Responder used to issue certificates to network devices, such as routers and switches. True...

A: Online responder (OR):A position service which allows customers to test the revocation status of a c...

Q: Write one Python statement to print the square root of a variable x. Control of decimals not require...

A: Click to see the answer

Q: What is the Internet?

A: The Internet is a worldwide network of trillions of computers and other tools for online use. With t...

Q: In Book computer networking a top-down approach sixth edition 4.4 (Network Address Translation (NAT)...

A: NAT table stands for Network Address Translation table.It is used by routers to translate private IP...

Q: Question 5 and 7

A: Following questions ask to print the recursive function of the two arguments:Declaring the variable ...

Q: Create a query that uses any THREE of the tables (SCHOOL, DEPARTMENT, PROFESSOR, TECHNOLOGY & CO...

A: Writing a query based on the given tables by using arithmetic and special operators in the condition...