Code:
#include <stdio.h>
int remainder ;
int addint (int x, int y); /*Function Prototypes for actions*/
int subint (int x, int y); /* to be performed on input */
int multint (int x, int y);
int divideint (int x, int y, int z);
int powerint (int x, int y);
int gcdint (int x, int y);
int factorial (int x);
int main()
{
int x=0; /* Variable initializations */
int y=0;
int z=0;
char select;
printf("This program will perform one of the following operations \n"); /* User Menu for program */
printf("to two numbers based on your selection from the following menu: \n \n");
printf("1. Add two integers. \n");
printf("2. Subtract two integers. \n");
printf("3. Multiply two integers. \n");
printf("4. Divide two integers. \n");
printf("5. Calculate integer raised to a positive power. \n");
printf("6. Compute greatest common divisor of two integers. \n");
printf("7. Compute the factorial of an integer. \n \n");
printf("To quit the program at any time, please press q or Q. \n \n");
printf("Please enter two integers: \n\n"); /* Prompt user for two integers */
fflush(stdin);
scanf("%d", &x);
scanf("%d", &y);
printf("Please choose desired operation from above menu. \n "); /* Prompt user to select desired action */
fflush(stdin);
scanf("%c", &select);
while (select != 'q' && select != 'Q') /* Continues running program unitl sentinel value entered */
{
switch (select) /* Invoke the proper function to perform required action */
{
case '1':
printf("\n The sum of %d and %d is %d.", x, y,addint(x,y));
break;
case '2':
printf("\n %d minus %d is %d", x, y, subint(x,y));
break;
case '3':
printf("\n %d multiplied by %d is %d", x, y, multint(x,y));
break;
case '4':
if(y >0)
printf("\n %d divided by %d is %d and remainder is %d", x,y,divideint(x,y,z));
else /*Else statement to handle division by 0 */
printf("\n Error, division by 0. \n");
break;
case '5':
if (y>=0)
printf("\n %d raised to power of %d is %d ", x, y, powerint(x,y));
else /*Else statement to handle raising to a negative power */
printf("\n You cannot raise a number to a negative power!\n");
break;
case '6':
printf("\n The greatest common denominator of %d and %d is %d", x, y, gcdint(x,y));
break;
case '7':
if (x<0 || y<0) /*if statement to handle a negative factorial */
printf("\n Error, negative factorial value entered. \n");
else
printf("\n The value of the factorial for %d is %d \n", x, factorial(x));
printf("\n The value of the factorial for %d is %d", y, factorial(y));
break;
default:
printf("Incorrect value entered, please reenter. \n \n");
break;
}
printf("\n \n This program will perform one of the following operations \n"); /* Reposts "menu" and reprompts user for input */
printf("to two numbers based on your selection from the following menu: \n \n");
printf("1. Add two integers. \n");
printf("2. Subtract two integers. \n");
printf("3. Multiply two integers. \n");
printf("4. Divide two integers. \n");
printf("5. Calculate integer raised to a positive power. \n");
printf("6. Compute greatest common divisor of two integers. \n");
printf("7. Compute the factorial of an integer. \n \n");
printf("To quit the program, please press q or Q. \n \n");
printf("Please enter two integers: \n\n");
fflush(stdin);
scanf("%d", &x);
scanf("%d", &y);
printf("Please choose desired operation from above menu. \n ");
fflush(stdin);
scanf("%c", &select);
}
printf("You have terminated the program.\n");
return 0;
}
/* Function Definitions */
int addint (int x, int y)
{
return x + y;
}
int subint (int x, int y)
{
return x - y;
}
int multint (int x, int y)
{
return x * y;
}
int divideint (int x, int y, int z)
{
if (y != 0)
{
return x/y;
return z=x%y;
}
}
int powerint (int x, int y)
{
if (y==0)
return 1;
else
return (x * powerint(x , y-1));
}
int gcdint (int x,int y)
{
if (y!=0)
return (gcdint(y, x%y));
else
return x;
}
int factorial (int x, int y)
{
if (x == 0)
return 1;
else
if (x >0)
return x * factorial(x-1);
if (y == 0)
return 1;
else
if (y>0)
return y * factorial(y-1);
}