Change the code in the posted for the program posted in the same module named “p
ID: 3728405 • Letter: C
Question
Change the code in the posted for the program posted in the same module named “prog” as follows and submit as program 6:
Create functions and function prototypes to replace each of the calculator actions. Prototype each function above main and write the function (definition) below main.
The input by the user and output statement of each calculation should stay in main and not be moved to a function except the output for function “iseven” which should be done in the function.
Each function will accept the value(s) supplied by the user as parameters and return the result in a return statement (not a printf statement except for “iseven”).
The result returned by each function should be outputted by a printf statement in main.
Create a function named display that contains: menu display statements, the user prompt to select an option, user input. This function should have no parameters and should return a character as the option chosen by the user.
The functions required are:
display(void);
double add(double, double);
double sub(double, double);
double mult(double, double);
double div(double, double);
int remain(int, int);
double square(double);
double sqroot(double);
int factorial(int);
void iseven(int);
int greaterint(double);
int lesserint(double);
#include<math.h>
#include<stdio.h>
int main(void)
{
char menuoption;
int intx, inty, answeri, count, n, fact;
double doublex, doubley, answerd;
//prompt user
do{
//menu
printf(" Operation Menu Option ");
printf("Add A ");
printf("Subtract S ");
printf("Multiply M ");
printf("Divide D ");
printf("Remainder R ");
printf("Squared V ");
printf("Square Root T ");
printf("Factorial F ");
printf("Is even E ");
printf("Greater integer G ");
printf("Less integer L ");
printf("Exit program X ");
printf("Please enter a menu option from the table above: ");
scanf("%c", &menuoption);
switch (menuoption)
{
//addition
case'a': case'A':
printf("Please enter the first number: ");
scanf("%lf", &doublex);
printf("Please enter the second number: ");
scanf("%lf", &doubley);
answerd= doublex + doubley;
printf("Answer = %.2lf", answerd);
break;
//subtraction
case 's': case'S':
printf("Please enter the first number:");
scanf("%lf", &doublex);
printf("Please enter the second number:");
scanf("%lf", &doubley);
answerd= doublex-doubley;
printf("Answer = %.2lf", answerd);
break;
//multiplication
case 'm': case'M':
printf("Please enter the first number:");
scanf("%lf", &doublex);
printf("Please enter the second number:");
scanf("%lf", &doubley);
answerd= doublex * doubley;
printf("Answer = %.2f ", answerd);
break;
//division
case 'd': case'D':
printf("Please enter the first number:");
scanf("%lf", &doublex);
printf("Please enter the second number:");
scanf("%lf", &doubley);
if (doubley == 0) //checking if denominator is zero
printf("invalid, cannot divide by zero ");
else
{
answerd= doublex / doubley;
printf("Answer = %.2f ", answerd);
}
break;
//remainder
case 'r': case'R':
printf("Please enter the first number:");
scanf("%d", &intx);
printf("Please enter the second number:");
scanf("%d", &inty);
answeri= intx % inty;
printf("Answer = %d ", answeri);
break;
//squared
case 'v': case'V':
printf("Please enter the number to be squared:");
scanf("%lf", &doublex);
answerd= doublex*doublex;
printf("Answer = %.2lf", answerd);
break;
//square root
case 't': case'T':
printf("Please enter the number to take the square root of:");
scanf("%lf", &doublex);
if (doublex < 0) // checking is number for square root is negative
printf("invalid, cannot square root negative number ");
else
{
answerd= sqrt(doublex);
printf("Answer = %.2f ", answerd);
}
break;
//factorial
case 'f': case'F':
fact = 1;
printf("Please enter the number to take the factorial:");
scanf("%d", &n);
if (n<0)
printf("Cannot take the factorial of a negative number.");
else if (n>0)
{
for (count= n ; count>= 1; count--)
{
fact = fact * count;
}
printf("Factorial = %d", fact );
}
else
printf("The factorial of 0 is 1.");
break;
//even
case 'e': case'E':
printf("Please enter the first number:");
scanf("%d", &intx);
answeri= intx % 2;
if (answeri==0)
printf("The number is even. ");
else
printf("The number is not even. ");
break;
//greater integer
case 'g': case'G':
printf("Please enter the first number:");
scanf("%lf", &doublex);
answeri= ceil(doublex);
printf("Answer = %d", answeri);
break;
//lesser integer
case 'l': case'L':
printf("Please enter the first number:");
scanf("%d", &doublex);
answeri= floor(doublex) ;
printf("Answer = %d", answeri);
break;
//exit program
case 'x': case'X':
return 0;
break;
//improper input
default:
printf("Input not recognized. ");
}
} while (1);
return 0;
}
Explanation / Answer
Hello There,
PFB code in the requested format along with the output of test run:
prog6.c
------------------------------
#include <math.h>
#include <stdio.h>
void display(void);
double add(double, double);
double sub(double, double);
double mult(double, double);
double div(double, double);
int remain(int, int);
double square(double);
double sqroot(double);
int factorial(int);
void iseven(int);
int greaterint(double);
int lesserint(double);
int main(void)
{
char menuoption;
int intx, inty, answeri, count, n, fact;
double doublex, doubley, answerd;
//prompt user
do{
//menu
display();
scanf("%c", &menuoption);
switch (menuoption)
{
//addition
case'a': case'A':
printf("Please enter the first number: ");
scanf("%lf", &doublex);
printf("Please enter the second number: ");
scanf("%lf", &doubley);
answerd= add(doublex, doubley);
printf("Answer = %.2lf", answerd);
break;
//subtraction
case 's': case'S':
printf("Please enter the first number:");
scanf("%lf", &doublex);
printf("Please enter the second number:");
scanf("%lf", &doubley);
answerd= sub(doublex, doubley);
printf("Answer = %.2lf", answerd);
break;
//multiplication
case 'm': case'M':
printf("Please enter the first number:");
scanf("%lf", &doublex);
printf("Please enter the second number:");
scanf("%lf", &doubley);
answerd= mult(doublex, doubley);
printf("Answer = %.2f ", answerd);
break;
//division
case 'd': case'D':
printf("Please enter the first number:");
scanf("%lf", &doublex);
printf("Please enter the second number:");
scanf("%lf", &doubley);
if (doubley == 0) //checking if denominator is zero
printf("invalid, cannot divide by zero ");
else
{
answerd= div(doublex, doubley);
printf("Answer = %.2f ", answerd);
}
break;
//remainder
case 'r': case'R':
printf("Please enter the first number:");
scanf("%d", &intx);
printf("Please enter the second number:");
scanf("%d", &inty);
answeri= remain(intx, inty);
printf("Answer = %d ", answeri);
break;
//squared
case 'v': case'V':
printf("Please enter the number to be squared:");
scanf("%lf", &doublex);
answerd= square(doublex);
printf("Answer = %.2lf", answerd);
break;
//square root
case 't': case'T':
printf("Please enter the number to take the square root of:");
scanf("%lf", &doublex);
if (doublex < 0) // checking is number for square root is negative
printf("invalid, cannot square root negative number ");
else
{
answerd= sqroot(doublex);
printf("Answer = %.2f ", answerd);
}
break;
//factorial
case 'f': case'F':
fact = 1;
printf("Please enter the number to take the factorial:");
scanf("%d", &n);
if (n<0)
printf("Cannot take the factorial of a negative number.");
else if (n>0)
{
fact = factorial(n);
printf("Factorial = %d", fact );
}
else
printf("The factorial of 0 is 1.");
break;
//even
case 'e': case'E':
printf("Please enter the first number:");
scanf("%d", &intx);
iseven(intx);
break;
//greater integer
case 'g': case'G':
printf("Please enter the first number:");
scanf("%lf", &doublex);
answeri= greaterint(doublex);
printf("Answer = %d", answeri);
break;
//lesser integer
case 'l': case'L':
printf("Please enter the first number:");
scanf("%lf", &doublex);
answeri= lesserint(doublex) ;
printf("Answer = %d", answeri);
break;
//exit program
case 'x': case'X':
return 0;
break;
//improper input
default:
printf("Input not recognized. ");
}
} while (1);
return 0;
}
void display(){
printf(" Operation Menu Option ");
printf("Add A ");
printf("Subtract S ");
printf("Multiply M ");
printf("Divide D ");
printf("Remainder R ");
printf("Squared V ");
printf("Square Root T ");
printf("Factorial F ");
printf("Is even E ");
printf("Greater integer G ");
printf("Less integer L ");
printf("Exit program X ");
printf("Please enter a menu option from the table above: ");
}
double add(double x, double y){
double z;
z = x+y;
return z;
}
double sub(double x, double y){
double z;
z = x-y;
return z;
}
double mult(double x, double y){
double z;
z = x*y;
return z;
}
double div(double x, double y){
double z;
z = x/y;
return z;
}
int remain(int x, int y){
int z;
z = x%y;
return z;
}
double square(double x){
double z;
z = x*x;
return z;
}
double sqroot(double x){
double z;
z = sqrt(x);
return z;
}
int factorial(int n){
int fact = 1;
for (int count= n ; count>= 1; count--)
{
fact = fact * count;
}
return fact;
}
void iseven(int n){
int answeri = n%2;
if (answeri==0)
printf("The number is even. ");
else
printf("The number is not even. ");
}
int greaterint(double x){
int z;
z = ceil(x);
return z;
}
int lesserint(double x){
int z;
z = floor(x);
return z;
}
-----------------------------
Test Runs:
-----------------------------------
$ gcc prog6.c -lm
$ ./a.out
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: A
Please enter the first number: 2.0
Please enter the second number: 3.5
Answer = 5.50
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: s
Please enter the first number:2.0
Please enter the second number:3.5
Answer = -1.50
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: m
Please enter the first number:2.0
Please enter the second number:3.5
Answer = 7.00
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: d
Please enter the first number:2.0
Please enter the second number:3.5
Answer = 0.57
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: r
Please enter the first number:7
Please enter the second number:3
Answer = 1
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: v
Please enter the number to be squared:2.5
Answer = 6.25
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: t
Please enter the number to take the square root of:49
Answer = 7.00
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: f
Please enter the number to take the factorial:6
Factorial = 720
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: e
Please enter the first number:9
The number is not even.
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: g
Please enter the first number:6.8
Answer = 7
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: l
Please enter the first number:6.8
Answer = 6
Operation Menu Option
Add A
Subtract S
Multiply M
Divide D
Remainder R
Squared V
Square Root T
Factorial F
Is even E
Greater integer G
Less integer L
Exit program X
Please enter a menu option from the table above: x
$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.