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

Write an algorithm and translate it to a C program for a calculator that perform

ID: 3811504 • Letter: W

Question

Write an algorithm and translate it to a C program for a calculator that performs three (3) operations on 1-byte numbers: addition, subtraction, and multiplication. The type of operation performed depends on the input op_code as follows

if op_code=1 the calculator performs addition of the two 1-byte numbers and store the result in Sum

if op_code=2 the calculator performs subtraction of the two 1-byte numbers and store the result in Difference

if op_code=3 the calculator performs multiplication of the two 1-byte numbers and store the result in the PRODH and PRODL

if op_code is not equal to any of these values the system outputs Error=1

Explanation / Answer

Program:

#include <stdio.h>
#include<conio.h>
int binaryproduct(int, int);
main()
{

long binary1, binary2, multiply = 0;
int i = 0, remainder = 0, sum[20], subtract[20],binaryprod[20];
int op_code;
int digit, factor = 1;
printf(" Enter the first 8-bit binary number:        ");
scanf("%ld", &binary1);
printf("Enter the second 8-bit binary number:       ");
scanf("%ld", &binary2);
printf("Enter operation to be performed: 1 for addtion, 2 for subtraction and 3 for multiplication: ");
scanf("%d", &op_code);
if (op_code == 1)
{
    while (binary1 != 0 || binary2 != 0)
       {
       sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
       remainder =(binary1 % 10 + binary2 % 10 - remainder) / 2;
       binary1 = binary1 / 10;
       binary2 = binary2 / 10;
    }
    if (remainder != 0)
       sum[i++] = remainder;

    --i;
    printf("Sum of two binary numbers:        ");
    while (i >= 0)
       printf("%d", sum[i--]);
}
if (op_code == 2)
{
    while (binary1 != 0 || binary2 != 0)
       {
       if((binary1 % 10) >= (binary2 % 10))
      {
      subtract[i++] =(binary1 % 10 - binary2 % 10 - remainder) % 2;
          remainder =(binary1 % 10 + binary2 % 10 + remainder) % 2;
          }
   else
      {
      subtract[i++] =(binary2 % 10 - binary1 % 10 - remainder) % 2;
          remainder =(binary1 % 10 + binary2 % 10 + remainder) % 2;
          }
   binary1 = binary1 / 10;
       binary2 = binary2 / 10;
   }
    if (remainder != 0)
       subtract[i++] = remainder;

    --i;
    printf("Subtraction of two binary numbers:        ");
    while (i >= 0)
       printf("%d", subtract[i--]);
}
if (op_code == 3)
{
    while (binary1 != 0 || binary2 != 0)
       {
        digit = binary2 % 10;
        if (digit == 1)
        {
            binary1 = binary1 * factor;
            multiply = binaryproduct(binary1, multiply);
        }
        else
            binary1 = binary1 * factor;
        binary2 = binary2 / 10;
        factor = 10;
    }
    printf("Product of two binary numbers: %ld", multiply);
  
}
return 0;
getch();
getch();
}
int binaryproduct(int binary1,int binary2)
{
    int i = 0, remainder = 0, summ[20];
    int binaryprod = 0;
    while (binary1 != 0 || binary2 != 0)
    {
        summ[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
        binary1 = binary1 / 10;
        binary2 = binary2 / 10;
    }
    if (remainder != 0)
        summ[i++] = remainder;
    --i;
    while (i >= 0)
        binaryprod = binaryprod * 10 + summ[i--];
    return binaryprod;
}

Output:

Enter the first 8-bit binary number:          10101010

Enter the second 8-bit binary number:      01010101

Enter operation to be performed:

1 for addition,

2 for subtraction and

3 for multiplication:

1

Sum of two binary numbers:     11111111

Enter the first 8-bit binary number:          10101010

Enter the second 8-bit binary number:      01010101

Enter operation to be performed:

1 for addition,

2 for subtraction and

3 for multiplication:

2

Subtraction of two binary numbers:       01010101

Enter the first 8-bit binary number:          10101010

Enter the second 8-bit binary number:      01010101

Enter operation to be performed:

1 for addition,

2 for subtraction and

3 for multiplication:

3

Product of two binary numbers:    111000 01110010

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