(PROGRAMMING IN C) Write a program which can accept binary numbers and certain o
ID: 3807737 • Letter: #
Question
(PROGRAMMING IN C)
Write a program which can accept binary numbers and certain operations to calculate the results of those operations. The user should see something like:
Enter a 16 bit unsigned binary number:
Enter operator (+, -, *, %, /, <<, >>, |, &, ^, E)
Enter a 16 bit unsigned binary number:
The answer is: (note: the answer should be displayed in both binary and decimal)
Enter operator (+, -, *, %, /, <<, >>, |, &, ^, E)
Enter a 16 bit unsigned binary number:
The answer is: (note: this should perform the operation on the previous answer and the newly entered number)
The program should continue in this manner, until E is entered as the operator. When E is entered, the program should terminate. Hints: partition up the problem.
Create subroutines to:
• Accept input from the user and verify that they have entered a 16 bit binary number (note: you’ll need to handle binary numbers as strings) • Convert a string holding a binary number to an unsigned integer
• Convert an unsigned integer to a string holding a binary representation of that number
• Accept two numbers and an operator, perform the noted operation (hint: only capture the first < from << or > from >>, doing so will allow you to handle the operator as a simple character) Hint #2: use fgets and sscanf to take user input. scanf alone will be tough to get correct.
Explanation / Answer
#include // Needed to use IO functions using namespace std; int main() { int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int upperbound; // Sum from 1 to this upperbound int absDiff; // The absolute difference between the two sums // Prompt user for an upperbound cout > upperbound; // Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound int number = 1; while (number sumEven) { absDiff = sumOdd - sumEven; } else { absDiff = sumEven - sumOdd; } // Print the results coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.