Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ conversion from Decimal to Binary and Binary to Decimal. Please help...thank

ID: 3562883 • Letter: C

Question

C++ conversion from Decimal to Binary and Binary to Decimal. Please help...thank you!!

Create a program which can maintain binary numbers, (being able to add and subtract them), as well as being able to convert them from binary to decimal, and decimal to binary. You'll need to make your own custom class to do this, as your code needs to be able to handle any kind of input. My suggestion is that your binary class use 'char' for each of the binary digits. To keep the assignment from getting too complicated, you'll never need more than SIX binary digits. so consider that the upper limit of how many char variables you'll need in each binary class. Your code will need to perform the following feats: Convert 34 to Binary Convert 22 to Binary Convert 1001 to Decimal Convert 111011 to Decimal Add 11 and 1110 in Binary, Return the Answer in Binary Add 1010 and 111 in Binary, then Convert the Answer to Decimal A general reminder, double-check your code's output to make it's correctly converting. And although many compilers offer built-in Binary-to-Decimal/Decimal-to-Binary converters. PLEASE DO NOT USE THESE BUILT IN CONVERTERS, as I will intentionally remove points for doing so.

Explanation / Answer

#include #include int binary_decimal(int n); int decimal_binary(int n); int main() { int n; char c; printf("Instructions: "); printf("1. Enter alphabet 'd' to convert binary to decimal. "); printf("2. Enter alphabet 'b' to convert decimal to binary. "); scanf("%c",&c); if (c =='d' || c == 'D') { printf("Enter a binary number: "); scanf("%d", &n); printf("%d in binary = %d in decimal", n, binary_decimal(n)); } if (c =='b' || c == 'B') { printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %d in binary", n, decimal_binary(n)); } return 0; } int decimal_binary(int n) /* Function to convert decimal to binary.*/ { int rem, i=1, binary=0; while (n!=0) { rem=n%2; n/=2; binary+=rem*i; i*=10; } return binary; } int binary_decimal(int n) /* Function to convert binary to decimal.*/ { int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote