Code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159
int main(void)
{
double sum, first_Num, sec_Num, third_Num, diff, product, quotient, srt, area, rad1, rad2, base, height;
int choice, selection;
do
{
printf("\nChoose what calculation to perform\n");
printf("\t1 - Addition\n");
printf("\t2 - Subtration\n");
printf("\t3 - Multiplication\n");
printf("\t4 - Division\n");
printf("\t5 - Square Root\n");
printf("\t6 - Exponents\n");
printf("\t7 - Absolute Value\n");
printf("\t8 - Area of Shapes\n");
printf("(Enter a zero to exit) \n");
printf("\nMake a selection: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
sum = first_Num + sec_Num;
printf("\nThe sum of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, sum);
break;
}
case 2:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
diff = first_Num - sec_Num;
printf("\nThe difference of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, diff);
break;
}
case 3:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
product = first_Num * sec_Num;
printf("\nThe product of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, product);
break;
}
case 4:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
quotient = first_Num / sec_Num;
printf("\nThe quotient of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, quotient);
break;
}
case 5:
{
printf("\nEnter number: ");
scanf("%lf", &srt);
{
while (srt <= 0)
{
printf("ERROR!! Value must be a positive number\n\n");
printf("Enter number: ");
scanf("%lf", &srt);
}
printf("\nThe square root of %.4f is %.4f\n", srt, sqrt(srt));
}
break;
}
case 6:
{
printf("\nEnter base number: ");
scanf("%lf", &first_Num);
printf("Enter exponent: ");
scanf("%lf", &sec_Num);
printf("\n%.4f to the power of %.4f is %.4f\n\n", first_Num, sec_Num, pow(first_Num, sec_Num));
break;
}
case 7:
{
printf("\nEnter number: ");
scanf("%lf", &first_Num);
printf("\nThe absolute value of %.4f is %.4f\n\n", first_Num, fabs(first_Num));
break;
}
case 8:
{
do
{
printf("\nFIND THE AREA OF A SHAPE\n\n");
printf("Which shape?\n");
printf("\t1 - Square\n");
printf("\t2 - Rectangle\n");
printf("\t3 - Parallelogram\n");
printf("\t4 - Trapezoid\n");
printf("\t5 - Circle\n");
printf("\t6 - Ellipse\n");
printf("\t7 - Triangle\n");
printf("\t8 - Go back to previous menu\n");
printf("\nMake a selection: ");
scanf("%d", &selection);
switch (selection)
{
case 1:
{
printf("\nArea Of A Square\n");
printf("\nEnter length of a side of the square: ");
scanf("%lf", &first_Num);
while (first_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter length of a side of the square: ");
scanf("%lf", &first_Num);
}
area = first_Num * first_Num;
printf("\nThe area of a square with a side of %.4f is %.4f\n\n", first_Num, area);
break;
}
case 2:
{
printf("\nArea Of A Rectangle\n");
printf("\nEnter width: ");
scanf("%lf", &first_Num);
while (first_Num <=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter width: ");
scanf("%lf", &first_Num);
}
printf("Enter height: ");
scanf("%lf", &sec_Num);
while(sec_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &sec_Num);
}
area = first_Num * sec_Num;
printf("\nThe area of a rectagle with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area);
break;
}
case 3:
{
printf("\nArea Of A Parallelogram");
printf("\nEnter base: ");
scanf("%lf", &first_Num);
while (first_Num <=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base: ");
scanf("%lf", &first_Num);
}
printf("\nEnter height: ");
scanf("%lf", &sec_Num);
while(sec_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &sec_Num);
}
area = first_Num * sec_Num;
printf("\nThe area of a parallelogram with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area);
break;
}
case 4:
{
printf("\nArea Of A Trapezoid");
printf("\nEnter base-1: ");
scanf("%lf", &first_Num);
while (first_Num <=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base-1: ");
scanf("%lf", &first_Num);
}
printf("\nEnter base-2: ");
scanf("%lf", &sec_Num);
while(sec_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base-2: ");
scanf("%lf", &sec_Num);
}
printf("\nEnter height: ");
scanf("%lf", &third_Num);
while(third_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &third_Num);
}
area = (third_Num/2) * (first_Num + sec_Num);
printf("\nThe area of a trapezoid with a base-1 of %.4f, a base-2 %.4f, and a height of %.4f is %.4f\n\n", first_Num, sec_Num, third_Num, area);
break;
}
case 5:
{
printf("\nArea Of A Circle\n");
printf("\nEnter the radius: ");
scanf("%lf", &rad1);
while(rad1 <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter the radius: ");
scanf("%lf", &rad1);
}
area = (rad1 * rad1) * PI;
printf("The area of a circle with a radius of %.4f is %.4f\n\n", rad1, area);
break;
}
case 6:
{
printf("\nArea Of An Ellipse");
printf("\nEnter radius-1: ");
scanf("%lf", &rad1);
while(rad1 <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter radius-1: ");
scanf("%lf", &rad1);
}
printf("\nEnter radius-2: ");
scanf("%lf", &rad2);
while(rad2 <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter radius-2: ");
scanf("%lf", &rad2);
}
area = (rad1 * rad2) * PI;
printf("The area of an ellipse with a radius-1 of %.4f and a radius-2 of %.4f is %.4f", rad1, rad2, area);
break;
}
case 7:
{
printf("\nArea Of A Triangle");
printf("\nEnter base: ");
scanf("%lf", &base);
while(base<=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base: ");
scanf("%lf", &base);
}
printf("Enter height: ");
scanf("%lf", &height);
while(height<=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &height);
}
area = (base * height) / 2;
printf("The area of a triangle with a base of %.4f and a height of %.4f is %.4f", base, height, area);
break;
}
default:
{
if (selection !=8)
{
printf("\nYour selection was not from the menu\n");
printf("Please try again.\n\n");
}
break;
}
}
break;
}
while (selection != 8);
}
default:
{
if (choice != 0)
{
printf("\nYour selection was not from the menu\n");
printf("Please try again.\n\n");
}
break;
}
}
}
while (choice != 0);
return (0);
}