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:

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.
a) The following program is supposed to convert a binary number to its decimal equivalent. Modify...
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...
In C Once you're certain that you understand what the program is doing, modify it so that it: asks for the number of faculty in addition to the number of students. prints the ratio of students to faculty (truncated at 1 decimal place). Test your modified program with several different inputs. Ensure that it works even if the ratio of students to faculty is not a whole number! #include <stdio.h> int main(void) { int nStudents = 0; /* Initialization, required...
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...
Can i get help with this C program requirement. Modify the program Figure 2.13 (letter/while). Create a variable at the top of your code named power2 and assign it the value 8 Create a variable at the top of your code named result and assign it the value 0 Ask the user to enter FOUR 1's and 0's followed by a '*' In the loop, when the letter is '1' add power2 to result and store it back into result...
Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and "Creating a Flowchart" assignments previously implemented in C. Compare and contrast the C and MATLAB versions of your codes. convert this to a matlab program #include <stdio.h> int main() { int a; printf("Enter first number: "); scanf("%d", &a); int b; printf("Enter second number: "); scanf("%d", &b); int limit; printf("Enter limit: "); scanf("%d", &limit); printf("\nFirst number: %d\n", a); printf("Second number: %d\n", b); int c; c...
Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...
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)...
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...
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 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...