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

[Part a] Consider a C program that reads two real numbers from the keyboard foll

ID: 3557928 • Letter: #

Question

[Part a] Consider a C program that reads two real numbers from the keyboard followed by a character where the character can be one of the operators +, -, *, /, or % providing the addition, subtraction, multiplication, division, or remainder respectively. Then the result is displayed on the screen.

For example, if the user types: 2.0 3.0 % then your code will display: 2 % 3 = 2

Note: In the above example the inputs are real numbers but the remainder only performs an integer operation. Hence, the two real numbers must be type cast to allow computation by remainder. In the case where an invalid operator is entered (e.g. $) then an error message is printed to the standard output, e.g. Invalid entry! Note: Your program must employ a loop such to prompt user by asking whether the user wants to perform another computation or desires to terminate the program. Write the above program by ONLY using if, else if, and/or else statements. ] Write the above program by ONLY using switch. []

[Part b] Write a C program that reads an integer from the keyboard and prints its factorial to the standard output.

[Part c] Combine your program from Part a (where you used the switch) and Part b by adding a menu that allows to select between computation of two numbers [Part a] or calculating a factorial [Part b].

Explanation / Answer

#include<Stdio.h>
#include<stdlib.h>
int fact(int d)
{
if(d==1)
return 1;
else
return d*fact(d-1);
}

int main()
{
int a,b;
int result;
char c;
int choice;
printf(" enter 1 for calculating math operations enter 2 for finding factorial ");
printf(" choice : ");
scanf("%d",&choice);
if(choice==1)
{


printf(" input first integer : ");
scanf("%d",&a);
fflush(stdin);
printf(" input second integer : ");
scanf("%d",&b);
fflush(stdin);
printf(" enter the operator : ");
scanf("%c",&c);

if(c=='+')
{

result=a+b;
printf("%d + %d = %d. ",a,b,result);
}
else if(c=='-')
{
result=a-b;

printf("%d - %d= %d. ",a,b,result);
}
else if (c=='*')
{
result=a*b;
printf("%d * %d= %d. ",a,b,result);
}
else if (c=='%')
{
result=a%b;
printf("%d % %d = %d ",a,b,result);

}
else if(c=='/')
{
float c,d;
c=(float)a;
d=(float)b;
float result1;
result1=c/d;
printf("%f / %f = %f. ",c,d,result1);
}
else
printf(" error in operator input. ");
}
else if (choice==2)
{
printf("enter the number : ");
scanf("%d",&a);
printf(" factorial is : %d ",fact(a));
}

else

printf("wrong choice.good bye. ");

}

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