Skip to content

Instantly share code, notes, and snippets.

@ayitinya
Last active May 4, 2022 21:42
Show Gist options
  • Save ayitinya/f0c0213fba768ba5ab9896204e976455 to your computer and use it in GitHub Desktop.
Save ayitinya/f0c0213fba768ba5ab9896204e976455 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int sumAvg(int, int);
void bubbleSort(int *, int);
int min(int *, int);
int max(int *, int);
int main()
{
printf("Enter an integer followed by the return key\n");
int arr[3];
bool correctInput = false;
while (correctInput == false)
{
for (int i = 0; i < 3; i++)
{
scanf("%d", &arr[i]);
if (arr[i] < 0)
{
printf("Negative numbers are not allowed\n");
i--;
continue;
}
if (i > 0)
{
for (int j = i - 1; j >= 0; j--)
{
if (arr[j] == arr[i])
{
printf("Numbers cannot be repeated \n");
i--;
continue;
}
}
}
}
correctInput = true;
}
bubbleSort(arr, 3);
sumAvg(min(arr, 3), max(arr, 3));
return 0;
}
int sumAvg(int x, int y)
{
int sum = 0;
int numberOfElements = 0;
for (; x <= y; x++)
{
numberOfElements++;
sum += x;
}
float avg = (float)sum / (float)numberOfElements;
printf("%f", avg);
}
void bubbleSort(int *arr, int size)
{
bool swapped = false;
int i = 0;
do
{
swapped = false;
for (int j = 0; j < (size - 1 - i); j++)
{
if (arr[j] < arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
i++;
} while (swapped);
}
int min(int *arr, int size)
{
return arr[size - 1];
}
int max(int *arr, int size)
{
return arr[0];
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
unsigned long long int factorial(int);
unsigned int combinations(unsigned int, unsigned int);
int main(void)
{
int n, r;
printf("Enter the value of n followed by r\n");
bool input = false;
while (input == false)
{
scanf("%d%d", &n, &r);
if (n >= r && n > 0 && r > 0)
{
input = true;
}
if (n < r)
{
printf("n must be greater than r\n");
}
if (n < 0 || r < 0)
{
printf("n and r must be positive numbers\n");
}
}
printf("C(%d,%d) = %d\n", n, r, combinations(n, r));
return 0;
}
unsigned long long int factorial(int n)
{
if (n < 2)
{
return 1;
}
return n * factorial(n - 1);
}
unsigned int combinations(unsigned int n, unsigned int r)
{
return (factorial(n) * pow(r, n)) / (factorial(n - r) * factorial(r));
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
unsigned long long int factorial(int);
unsigned int permutations(unsigned int, unsigned int);
int main(void)
{
int n, r;
printf("Enter the value of n followed by r\n");
bool input = false;
while (input == false)
{
scanf("%d%d", &n, &r);
if (n >= r && n > 0 && r > 0)
{
input = true;
}
if (n < r)
{
printf("n must be greater than r\n");
}
if (n < 0 || r < 0)
{
printf("n and r must be positive numbers\n");
}
}
printf("C(%d,%d) = %d\n", n, r, permutations(n, r));
return 0;
}
unsigned long long int factorial(int n)
{
if (n < 2)
{
return 1;
}
return n * factorial(n - 1);
}
unsigned int permutations(unsigned int n, unsigned int r)
{
return (factorial(n) * sqrt(r - 1)) / factorial(n - r);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void findAdjacent(float, float);
void findOpposite(float, float);
float degreeToRadians(float);
int main(void)
{
printf("Enter the angle followed by the hypotenuse\n");
float theta = 0.0f, hypotenuse = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &theta, &hypotenuse);
if (theta < 45 && theta >= 0 && hypotenuse > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
findAdjacent(degreeToRadians(theta), hypotenuse);
findOpposite(degreeToRadians(theta), hypotenuse);
return 0;
}
void findAdjacent(float theta, float hypotenuse)
{
printf("The adjacent is %f\n", cosf(theta) * hypotenuse);
return;
}
void findOpposite(float theta, float hypotenuse)
{
printf("The opposite is %f\n", sinf(theta) * hypotenuse);
return;
}
float degreeToRadians(float theta)
{
return theta * (M_PI / 180.0);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void findAdjacent(float, float);
void findHypotenuse(float, float);
float degreeToRadians(float);
int main(void)
{
printf("Enter the angle followed by the hypotenuse\n");
float theta = 0.0f, opposite = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &theta, &opposite);
if (theta < 45 && theta >= 0 && opposite > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
findHypotenuse(degreeToRadians(theta), opposite);
findAdjacent(degreeToRadians(theta), opposite);
return 0;
}
void findAdjacent(float theta, float opposite)
{
printf("The adjacent is %f\n", opposite / tanf(theta));
return;
}
void findHypotenuse(float theta, float opposite)
{
printf("The hypotenuse is %f\n", opposite / sinf(theta));
return;
}
float degreeToRadians(float theta)
{
return theta * (M_PI / 180.0);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void findOpposite(float, float);
void findHypotenuse(float, float);
float degreeToRadians(float);
int main(void)
{
printf("Enter the angle followed by the hypotenuse\n");
float theta = 0.0f, adjacent = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &theta, &adjacent);
if (theta < 45 && theta >= 0 && adjacent > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
findHypotenuse(degreeToRadians(theta), adjacent);
findOpposite(degreeToRadians(theta), adjacent);
return 0;
}
void findOpposite(float theta, float adjacent)
{
printf("The opposite is %f\n", adjacent * tanf(theta));
return;
}
void findHypotenuse(float theta, float adjacent)
{
printf("The hypotenuse is %f\n", adjacent / cosf(theta));
return;
}
float degreeToRadians(float theta)
{
return theta * (M_PI / 180.0);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void findAngle(float, float);
void findHypotenuse(float, float);
int main(void)
{
printf("Enter the opposite followed by the adjacent\n");
float opposite = 0.0f, adjacent = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &opposite, &adjacent);
if (opposite > 0 && adjacent > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
findAngle(opposite, adjacent);
findHypotenuse(opposite, adjacent);
return 0;
}
void findAngle(float opposite, float adjacent)
{
printf("The angle is %f\n", atan2(opposite, adjacent) * (180.0 / M_PI));
return;
}
void findHypotenuse(float opposite, float adjacent)
{
printf("The hypotenuse is %f\n", hypotf(opposite, adjacent));
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void findAngle(float, float);
void findOpposite(float, float);
int main(void)
{
printf("Enter the opposite followed by the hypotenuse\n");
float adjacent = 0.0f, hypotenuse = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &adjacent, &hypotenuse);
if (adjacent > 0 && hypotenuse > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
findAngle(adjacent, hypotenuse);
findOpposite(adjacent, hypotenuse);
return 0;
}
void findAngle(float adjacent, float hypotenuse)
{
printf("The angle is %f\n", acosf(adjacent / hypotenuse) * (180.0 / M_PI));
return;
}
void findOpposite(float adjacent, float hypotenuse)
{
printf("The opposite is %f\n", hypotenuse * sinf(acosf(adjacent / hypotenuse)));
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void findAngle(float, float);
void findAdjacent(float, float);
int main(void)
{
printf("Enter the opposite followed by the hypotenuse\n");
float opposite = 0.0f, hypotenuse = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &opposite, &hypotenuse);
if (opposite > 0 && hypotenuse > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
findAngle(opposite, hypotenuse);
findAdjacent(opposite, hypotenuse);
return 0;
}
void findAngle(float opposite, float hypotenuse)
{
printf("The angle is %f\n", asinf(opposite / hypotenuse) * (180.0 / M_PI));
return;
}
void findAdjacent(float opposite, float hypotenuse)
{
printf("The adjacent is %f\n", hypotenuse * cosf(asinf(opposite / hypotenuse)));
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
float findAngle(float *, float *);
float angleToDegrees(float);
int main(void)
{
printf("Enter the opposite followed by the adjacent\n");
float opposite = 0.0f, adjacent = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &opposite, &adjacent);
if (opposite > 0 && adjacent > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
printf("The angle in radians is %f\n", findAngle(&opposite, &adjacent));
printf("The angle in degrees is %f\n", angleToDegrees(findAngle(&opposite, &adjacent)));
return 0;
}
float findAngle(float *opposite, float *adjacent)
{
return atan2(*opposite, *adjacent);
}
float angleToDegrees(float angle)
{
return angle * (180.0 / M_PI);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
float findAngle(float *, float *);
float angleToDegrees(float);
int main(void)
{
printf("Enter the adjacent followed by the hypotenuse\n");
float adjacent = 0.0f, hypotenuse = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &adjacent, &hypotenuse);
if (adjacent > 0 && hypotenuse > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
printf("The angle in radians is %f\n", findAngle(&adjacent, &hypotenuse));
printf("The angle in degrees is %f\n", angleToDegrees(findAngle(&adjacent, &hypotenuse)));
return 0;
}
float findAngle(float *adjacent, float *hypotenuse)
{
return acosf(*adjacent / *hypotenuse);
}
float angleToDegrees(float angle)
{
return angle * (180.0 / M_PI);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
float findAngle(float *, float *);
float angleToDegrees(float);
int main(void)
{
printf("Enter the opposite followed by the hypotenuse\n");
float opposite = 0.0f, hypotenuse = 0.0f;
bool input = false;
while (input == false)
{
scanf("%f%f", &opposite, &hypotenuse);
if (opposite > 0 && hypotenuse > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
printf("The angle in radians is %f\n", findAngle(&opposite, &hypotenuse));
printf("The angle in degrees is %f\n", angleToDegrees(findAngle(&opposite, &hypotenuse)));
return 0;
}
float findAngle(float *opposite, float *hypotenuse)
{
return asinf(*opposite / *hypotenuse);
}
float angleToDegrees(float angle)
{
return angle * (180.0 / M_PI);
}
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
void findC(float *, float *, float *);
float degreesToRadians(float);
int main(void)
{
float alpha = 0.0f, beta = 0.0f;
float A = 0.0f, B = 0.0f;
bool input = false;
printf("Enter the values of alpha, followed by beta and the length of A\nTerminate each with the enter key\n");
while (input == false)
{
scanf("%f%f%f", &alpha, &beta, &A);
if (alpha > 0 && beta > 0 && A > 0 && alpha + beta < 180)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
if (alpha + beta > 180)
{
printf("The sum of alpha and beta must be less than 180\n");
}
if (alpha < 0 || beta < 0 || A < 0)
{
printf("Values must be greater than 0\n");
}
}
}
findC(&alpha, &beta, &A);
return 0;
}
void findC(float *alpha, float *beta, float *A)
{
float gamma = 180 - (*alpha) - (*beta);
float C = (*A * sinf(degreesToRadians(gamma))) / (sinf(*alpha));
printf("The value of C is %f\n", C);
printf("The value of gamma is %f\n", gamma);
}
float degreesToRadians(float angle)
{
return angle * (M_PI / 180);
}
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
void findC(float *, float *, float *);
float degreesToRadians(float);
int main(void)
{
float alpha = 0.0f, gamma = 0.0f;
float A = 0.0f;
bool input = false;
printf("Enter the values of alpha, followed by gamma and the length of A\nTerminate each with the enter key\n");
while (input == false)
{
scanf("%f%f%f", &alpha, &gamma, &A);
if (alpha > 0 && gamma > 0 && A > 0 && alpha + gamma < 180)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
if (alpha + gamma > 180)
{
printf("The sum of alpha and gamma must be less than 180\n");
}
if (alpha < 0 || gamma < 0 || A < 0)
{
printf("Values must be greater than 0\n");
}
}
}
findC(&alpha, &gamma, &A);
return 0;
}
void findC(float *alpha, float *gamma, float *A)
{
float beta = 180 - (*alpha) - (*gamma);
float B = (*A * sinf(degreesToRadians(beta))) / (sinf(*alpha));
printf("The value of B is %f\n", B);
printf("The value of beta is %f\n", beta);
}
float degreesToRadians(float angle)
{
return angle * (M_PI / 180);
}
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
void findA(float *, float *, float *);
float degreesToRadians(float);
int main(void)
{
float beta = 0.0f, gamma = 0.0f;
float B = 0.0f;
bool input = false;
printf("Enter the values of beta, followed by gamma and the length of B\nTerminate each with the enter key\n");
while (input == false)
{
scanf("%f%f%f", &beta, &gamma, &B);
if (beta > 0 && gamma > 0 && B > 0 && beta + gamma < 180)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
if (beta + gamma > 180)
{
printf("The sum of beta and gamma must be less than 180\n");
}
if (beta < 0 || gamma < 0 || B < 0)
{
printf("Values must be greater than 0\n");
}
}
}
findA(&beta, &gamma, &B);
return 0;
}
void findA(float *beta, float *gamma, float *B)
{
float alpha = 180 - (*beta) - (*gamma);
float A = (*B * sinf(degreesToRadians(alpha))) / (sinf(degreesToRadians(*beta)));
printf("The value of A is %f\n", A);
printf("The value of alpha is %f\n", alpha);
}
float degreesToRadians(float angle)
{
return angle * (M_PI / 180);
}
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
float findA(float *, float *, float *);
float degreeToRadians(float);
int main(void)
{
float alpha = 0.0f, B = 0.0f, C = 0.0f;
printf("Enter the values of alpha, B and C: \n");
bool input = false;
while (input == false)
{
scanf("%f%f%f", &alpha, &B, &C);
if (alpha > 0 && B > 0 && C > 0 && alpha <= 180)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
if (alpha > 90)
{
printf("Alpha should be 90 or less\n");
}
if (alpha < 0 || B < 0 || C < 0)
{
printf("Values must be greater than 0\n");
}
}
}
printf("The value of A is %f\n", findA(&alpha, &B, &C));
return 0;
}
float degreeToRadians(float angle)
{
return angle * (M_PI / 180);
}
float findA(float *alpha, float *B, float *C)
{
return sqrt(pow(*B, 2) + pow(*C, 2) - 2 * (*B) * (*C) * cosf(degreeToRadians(*alpha)));
}
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
float findB(float *, float *, float *);
float degreeToRadians(float);
int main(void)
{
float beta = 0.0f, A = 0.0f, C = 0.0f;
printf("Enter the values of beta, A and C: \n");
bool input = false;
while (input == false)
{
scanf("%f%f%f", &beta, &A, &C);
if (beta > 0 && A > 0 && C > 0 && beta <= 180)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
if (beta > 90)
{
printf("beta should be 90 or less\n");
}
if (beta < 0 || A < 0 || C < 0)
{
printf("Values must be greater than 0\n");
}
}
}
printf("The value of B is %f\n", findB(&beta, &A, &C));
return 0;
}
float degreeToRadians(float angle)
{
return angle * (M_PI / 180);
}
float findB(float *beta, float *A, float *C)
{
return sqrt(pow(*A, 2) + pow(*C, 2) - 2 * (*A) * (*C) * cosf(degreeToRadians(*beta)));
}
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
float findC(float *, float *, float *);
float degreeToRadians(float);
int main(void)
{
float gamma = 0.0f, A = 0.0f, B = 0.0f;
printf("Enter the values of gamma, A and B: \n");
bool input = false;
while (input == false)
{
scanf("%f%f%f", &gamma, &A, &B);
if (gamma > 0 && A > 0 && B > 0 && gamma <= 180)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
if (gamma > 90)
{
printf("gamma should be 90 or less\n");
}
if (gamma < 0 || A < 0 || B < 0)
{
printf("Values must be greater than 0\n");
}
}
}
printf("The value of C is %f\n", findC(&gamma, &A, &B));
return 0;
}
float degreeToRadians(float angle)
{
return angle * (M_PI / 180);
}
float findC(float *gamma, float *A, float *B)
{
return sqrt(pow(*A, 2) + pow(*B, 2) - 2 * (*A) * (*B) * cosf(degreeToRadians(*gamma)));
}
#include <stdio.h>
#include <stdbool.h>
void functQuad(int *, int *);
void functXReflect(int *, int *);
void functYReflect(int *, int *);
void functSlope(int *, int *);
int main(void)
{
int x = 0, y = 0;
printf("Enter the values of x and y: \n");
bool input = false;
while (input == false)
{
scanf("%d%d", &x, &y);
if (x > 0 && y > 0)
{
input = true;
}
else
{
printf("Invalid input. Please try again.\n");
}
}
functQuad(&x, &y);
functXReflect(&x, &y);
functYReflect(&x, &y);
funcSlope(&x, &y);
return 0;
}
void functQuad(int *x, int *y)
{
if (*x == 0 || *y == 0)
{
printf("Undefined Quadrant\n");
}
if (*x > 0 && *y > 0)
{
printf("First Quadrant\n");
}
else if (*x > 0 && *y < 0)
{
printf("Second Quadrant\n");
}
else if (*x < 0 && *y < 0)
{
printf("Third Quadrant\n");
}
else if (*x < 0 && *y > 0)
{
printf("Fourth Quadrant\n");
}
}
void funcXReflect(int *x, int *y)
{
// multiply y by -1
printf("Reflection in X axis is (%d,%d)\n", *x, *y * -1);
}
void functYReflect(int *x, int *y)
{
// multiply x by -1
printf("Reflection in Y axis is (%d,%d)\n", *x * -1, *y);
}
void funcSlope(int *x, int *y)
{
// slope of line passing through (x,y) and (5, -5)
printf("Slope of line passing through (%d,%d) and (%d,%d) is %f\n", *x, *y, 5, -5, (float)(*y - (-5)) / (*x - 5));
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
unsigned long long int factorial(int);
float xFormula(unsigned int, unsigned int);
int main(void)
{
int n, r;
printf("Enter the value of n followed by r\n");
bool input = false;
while (input == false)
{
scanf("%d%d", &n, &r);
if (n >= r && n > 0 && r > 0)
{
input = true;
}
if (n < r)
{
printf("n must be greater than r\n");
}
if (n < 0 || r < 0)
{
printf("n and r must be positive numbers\n");
}
}
printf("X(%d,%d) = %d\n", n, r, xFormula(n, r));
return 0;
}
unsigned long long int factorial(int n)
{
if (n < 2)
{
return 1;
}
return n * factorial(n - 1);
}
float xFormula(unsigned int n, unsigned int r)
{
return ((factorial(n) * factorial(r)) / factorial(n - r)) * pow(n, sqrt(r));
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
unsigned long long int factorial(int);
float yFormula(unsigned int, unsigned int);
int main(void)
{
int n, r;
printf("Enter the value of n followed by r\n");
bool input = false;
while (input == false)
{
scanf("%d%d", &n, &r);
if (n >= r && n > 0 && r > 0)
{
input = true;
}
if (n < r)
{
printf("n must be greater than r\n");
}
if (n < 0 || r < 0)
{
printf("n and r must be positive numbers\n");
}
}
printf("Y(%d,%d) = %d\n", n, r, yFormula(n, r));
return 0;
}
unsigned long long int factorial(int n)
{
if (n < 2)
{
return 1;
}
return n * factorial(n - 1);
}
float yFormula(unsigned int n, unsigned int r)
{
return ((factorial(n)) / factorial(n - r)) * pow(n, sqrt(r));
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
unsigned long long int factorial(int);
float zFormula(unsigned int, unsigned int);
int main(void)
{
int n, r;
printf("Enter the value of n followed by r\n");
bool input = false;
while (input == false)
{
scanf("%d%d", &n, &r);
if (n >= r && n > 0 && r > 0)
{
input = true;
}
if (n < r)
{
printf("n must be greater than r\n");
}
if (n < 0 || r < 0)
{
printf("n and r must be positive numbers\n");
}
}
printf("Z(%d,%d) = %d\n", n, r, zFormula(n, r));
return 0;
}
unsigned long long int factorial(int n)
{
if (n < 2)
{
return 1;
}
return n * factorial(n - 1);
}
float zFormula(unsigned int n, unsigned int r)
{
return (n * (factorial(n - 1)) / factorial(r + 1)) * pow(n, sqrt(r));
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
void continueRunning(bool *);
int main(void)
{
printf("Choose a shape to find its volume\n(R)ectangular solid\n(Y)Cylinder\n(S)phere\n(C)one\n(Q)uit\n");
char shape = '\0';
bool running = true;
while (running == true)
{
scanf(" %c", &shape);
switch (toupper(shape))
{
case 'R':
float length, width, height;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the length, followed by the width and height of the rectangle\n");
scanf("%f %f %f", &length, &width, &height);
if (length > 0 && width > 0 && height > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The volume of the shape is %f\n", length * width * height);
continueRunning(&running);
break;
case 'Y':
float radius, height;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the radius and height of the cylinder\n");
scanf("%f %f", &radius, &height);
if (radius > 0 && height > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The volume of the shape is %f\n", M_PI * pow(radius, 2) * height);
continueRunning(&running);
break;
case 'S':
float radius;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the radius of the sphere\n");
scanf("%f", &radius);
if (radius > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Value cannot be negative\n");
}
}
printf("The volume of the shape is %f\n", 4.0 / 3.0 * M_PI * pow(radius, 3));
continueRunning(&running);
break;
case 'C':
float radius, height;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the radius and height of the cone\n");
scanf("%f %f", &radius, &height);
if (radius > 0 && height > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The volume of the shape is %f\n", M_PI * pow(radius, 2) * height / 3);
continueRunning(&running);
break;
case 'Q':
printf("Goodbye!\n");
running = false;
break;
default:
printf("Invalid shape\nTry again\n");
break;
}
}
return 0;
}
void continueRunning(bool *running)
{
printf("Do you want calculate the volume of another shape?\n");
printf("(Y)es or (N)o\n");
char answer = '\0';
scanf(" %c", &answer);
if (toupper(answer) == 'N')
{
*running = false;
}
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
void continueRunning(bool *);
int main(void)
{
printf("Choose a shape to find its volume\n(P)yramid\n(E)llipsoid\n(T)riangular prism\n(C)one\n(Q)uit\n");
char shape = '\0';
bool running = true;
while (running == true)
{
scanf(" %c", &shape);
switch (toupper(shape))
{
case 'P':
float length, width, height;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the length, followed by the width and height of the pyramid\n");
scanf("%f %f %f", &length, &width, &height);
if (length > 0 && width > 0 && height > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The volume of the pyramid is %f\n", (1.0 / 3.0) * length * width * height);
continueRunning(&running);
break;
case 'E':
float a, b, c;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the length, followed by the width and height of the ellipsoid\n");
scanf("%f %f %f", &a, &b, &c);
if (a > 0 && b > 0 && c > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The volume of the ellipsoid is %f\n", (4.0 / 3.0) * M_PI * a * b * c);
continueRunning(&running);
break;
case 'T':
float base, length, height;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the length, followed by the base and height of the triangular prism\n");
scanf("%f %f %f", &length, &base, &height);
if (length > 0 && base > 0 && height > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The volume of the triangular prism is %f\n", (1.0 / 2.0) * base * height * length);
break;
case 'C':
float radius, height;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the radius and height of the cone\n");
scanf("%f %f", &radius, &height);
if (radius > 0 && height > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
continueRunning(&running);
printf("The volume of the shape is %f\n", M_PI * pow(radius, 2) * height / 3);
break;
case 'Q':
printf("Goodbye!\n");
running = false;
break;
default:
printf("Invalid shape\nTry again\n");
break;
}
}
return 0;
}
void continueRunning(bool *running)
{
printf("Do you want calculate the volume of another shape?\n");
printf("(Y)es or (N)o\n");
char answer = '\0';
scanf(" %c", &answer);
if (toupper(answer) == 'N')
{
*running = false;
}
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
void continueRunning(bool *);
int main(void)
{
printf("Choose a shape to find its perimeter\n(S)quare\n(T)rapezoid\n(E)llipse\n(Q)uit\n");
char shape = '\0';
bool running = true;
while (running == true)
{
scanf(" %c", &shape);
switch (toupper(shape))
{
case 'S':
float length;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the length of the square\n");
scanf("%f", &length);
if (length > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The perimeter of the square is %f\n", 4 * length);
continueRunning(&running);
break;
case 'T':
float a, b, c, d;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the base and height of the trapezoid\n");
scanf("%f %f %f %f", &a, &b, &c, &d);
if (a > 0 && b > 0 && c > 0 && d > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The perimeter of the trapezoid is %f\n", a + b + c + d);
continueRunning(&running);
break;
case 'E':
float a, b;
bool inputValid = false;
while (inputValid == false)
{
printf("Enter the length and width of the ellipse\n");
scanf("%f %f", &a, &b);
if (a > 0 && b > 0)
{
inputValid = true;
}
else
{
printf("Invalid input. Values cannot be negative\n");
}
}
printf("The perimeter of the ellipse is %f\n", 2 * M_PI * sqrt((pow(a, 2) + pow(b, 2)) / 2));
continueRunning(&running);
break;
case 'Q':
printf("Goodbye!\n");
running = false;
break;
default:
printf("Invalid shape\nTry again\n");
break;
}
}
return 0;
}
void continueRunning(bool *running)
{
printf("Do you want calculate the area of another shape?\n");
printf("(Y)es or (N)o\n");
char answer = '\0';
scanf(" %c", &answer);
if (toupper(answer) == 'N')
{
*running = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment