slightly scuffed but dw about it
This commit is contained in:
parent
5856c0f3ea
commit
ccc4950e0c
1 changed files with 65 additions and 0 deletions
65
mafs.c
Normal file
65
mafs.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int ask_int_input(void) {
|
||||||
|
int a;
|
||||||
|
int result;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
printf("Enter a number: ");
|
||||||
|
result = scanf("%d", &a);
|
||||||
|
if (result == 1) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
printf("Input is not of type int. Try again\n");
|
||||||
|
while (getchar() != '\n')
|
||||||
|
;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_odd(int x) { return x % 2 == 0; }
|
||||||
|
|
||||||
|
int odd_or_even(void) {
|
||||||
|
int i = ask_int_input();
|
||||||
|
|
||||||
|
if (is_odd(i) == 1) {
|
||||||
|
printf("Number is even");
|
||||||
|
} else {
|
||||||
|
printf("Number is odd");
|
||||||
|
};
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
float ball_vol() {
|
||||||
|
double pi = 3.1415;
|
||||||
|
int a = ask_int_input();
|
||||||
|
double r = sqrt(a / (4 * pi));
|
||||||
|
double vol = 4 / 3 * pi * pow(r, 3);
|
||||||
|
printf("The volume of the ball is: %f\n", vol);
|
||||||
|
return vol;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Please give at least one argument\n");
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
if (argc > 2) {
|
||||||
|
printf("Too many arguments");
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
if (strcmp("help", argv[1]) == 0) {
|
||||||
|
printf("help command \n");
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
ball_vol();
|
||||||
|
odd_or_even();
|
||||||
|
};
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue