Write an algorithm and translate it to a C program for a calculator that perform
ID: 3812131 • 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 = 1Explanation / Answer
Code:-
#include <stdio.h>
int main(void) {
short int x,y,result;
short int mul_result=0;
short int PRODH=0;
int PRODL=0;
printf("Enter two 1-byte number one by one ");
scanf("%hhd",&x);
scanf("%hhd",&y);
printf("Enter op-code for performing operation :- 1 for addition 2 for subtraction 3 for multiplication");
int op_code;
scanf("%d",&op_code);
switch(op_code){
case 1:
result=x+y;
printf("The result of addition is %hhd",result);
break;
case 2:
result=x-y;
printf("The result of subtraction is %hhd",result);
break;
case 3:
mul_result=x*y;
PRODL=mul_result<<8;
PRODL=PRODL>>8;
if(mul_result<255)
PRODH=0;
else{
PRODH=PRODL>>8;
}
printf("The result of multiplication :- HIGHER BYTE = %hhd LOWER BYTE = %hhd",PRODH,PRODL);
break;
default:
printf("ERROR 1 ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.