Question

a) The following program is supposed to convert a binary number to its decimal equivalent. Modify the program so that it does

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer a) Code:

#include <stdio.h> // header file for basic input output operation
#include<math.h> // header file for math functions like pow()

int main() { // main fucntion
int binary; // variable that stores the binary number
printf("Enter a binary number:\n");
scanf("%d" , &binary); // user input
  
int nofDigit = 0, remain = binary, check = 0, tmp; // variable to store number of digits and remaining number number on integer division by 10
while(remain/10){
tmp = remain % 10;
remain = remain / 10;
// following contion is to set check flag 1 if the entered number is binary and set to 0 if entered number is not binary number
if(tmp == 0 || tmp == 1){
check = 1;
}else{
check = 0;
break; // break of any other integer is found other than 0 and 1
}
nofDigit++; // countung the digits present in the binary number
} // end of while loop
if (check == 0){ // check if user has entered binary number or not.
printf("Error: Binary number only contains 0s and 1s\n");
return 1;
}
int digit, dVal= 0, bx = binary; // variable to store digit in binary number and decimal value
for( int k =0; k<=nofDigit; k++){
digit = bx % 10; // getting the last digit in bx
bx /= 10; // dividing by 10
dVal += digit * pow(2,k); // calculating decimal value eg

} // end of for loop
printf("The decimal value of the binary number is %d\n", dVal); // printing the result
}

Smaple output:

root@kali:-/code# gcc b2d.c -o b2d - Im root@kali:-/code# ./b2d Enter a binary number: 1001 The decimal value of the binary n

Answer b:

gcc b2d.c -o b2d -lm

Explanation: we need to use the -lm flag to link the math library to the program if we do not use -lm(l for Long) flag we may get following error:

undefined reference to `pow'

error: ld returned 1 exit status

here gcc is the compiler and b2d.c is code file name and -o flag provide us the option to give the name of the output executable file if we do not use -o flag compiler will create a.out executable file in current working directory.

Note: Please let me know if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
a) The following program is supposed to convert a binary number to its decimal equivalent. Modify...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert a decimal fractional number 34/5 to its binary equivalent using a program to  evaluate the bits...

    Convert a decimal fractional number 34/5 to its binary equivalent using a program to  evaluate the bits of the fractional part for at least upto the second repeating block. i have written some code in python however it is not getting an output at all, so either fixing the code or writing another one would be beyond helpful def binary(n,k): n = 8.6 k = 3 #'n' is the fractional number #'k' is the number of bits up to the loop...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Draw the flowchart of the following program: # include < stdio.h> # include < math.h> int...

    Draw the flowchart of the following program: # include < stdio.h> # include < math.h> int main (void) {double time, velocity, acceleration; print f("Enter new time value in second: \n"); scanf("% 1f", & time); if (time = = 1)} velocity = 0.5 * pow (time, 3); acceleration = 0.5* velocity * velocity;); if (time = = 0.5 * pow (time, 3); acceleration = .5 velocity * velocity;} else {velocity = 1.5 * pow (time, 3); acceleration = 1.5 * velocity...

  • I'm working on a java program where I'm supposed to convert 4 bit binary into decimal....

    I'm working on a java program where I'm supposed to convert 4 bit binary into decimal. I went with if statements and tried to use an else to print out an error message if the user enters a number that isn't a binary number but it prints no matter what. (so, it prints that it's a vowel and that it's not a vowel) 1 import java.util.Scanner; 2 3 public class HomeworkTwoQ2 4 { 5 6 public static void main(String[] args)...

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • Please help with this. Modify the program Figure 2.6 (exam1/exam2). Ask the user to enter two...

    Please help with this. Modify the program Figure 2.6 (exam1/exam2). Ask the user to enter two numbers Find the result when the first number is divided by the second Also find the remainder when the first number is divided by the second In other words, the program used to do this: 68 85 score = 81 Now it should do this 22 2 Divide = 11 Remainder = 0 If you run it again: 11 2 Divide = 5 Remainder...

  • 1. [17 pts, 1/line] Modify the following code to display the binary information stored at a...

    1. [17 pts, 1/line] Modify the following code to display the binary information stored at a location in memory referred named myVar: #inciude <stdio.h> #define N 8 int main0 //Enter the number of bits here char myVar/Declare variable here int i; printf ("%c", * ( (long*) &myVar) &1<<i? ' 1 ' : ' 0 ' ) ; printf ("n" return 0

  • How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary;...

    How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary; int reminder, weight = 1; binary = 0.0; //Input decimal number from user printf("Enter any decimal number: "); scanf("%lld", &decimal); // Since we do not want change the value of decimal in the code // let us use tempDecimal to store the value of decimal tempDecimal = decimal; printf("\n\n"); // Let us use while loop first to implement printf("while loop part:\n"); /**************START FROM HERE...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT