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; }

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;

 

}

 

 

check_circle

Expert Answer

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

  &...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: What is pseudo code?

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

question_answer

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 ...

question_answer

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

A: Truth Table:

question_answer

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...

question_answer

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

question_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...

question_answer

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...

question_answer

Q: Question 5 and 7

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

question_answer

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

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