diff --git a/DeitelC/Chapter4/NaturalNumberArithmetic.c b/DeitelC/Chapter4/NaturalNumberArithmetic.c new file mode 100644 index 0000000..68a9426 --- /dev/null +++ b/DeitelC/Chapter4/NaturalNumberArithmetic.c @@ -0,0 +1,26 @@ +#include + +int main(void) { + int i = 0, total = 0; + puts("Enter an integer!"); + if(scanf("%d", &i)) { + for(int sum = 0; sum <= i; sum++) { + total += sum; + } + printf("Total sum: %d\n", total); + total = 0; + for(int sum = 0; sum * sum <= i * i; sum++) { + int pow = sum * sum; + total += pow; + } + printf("Sum of squares: %d\n", total); + total = 0; + for(int sum = 0; sum * sum * sum <= i * i * i; sum++) { + int pow = sum * sum * sum; + total += pow; + } + printf("Sum of cubes: %d\n", total); + } else { + puts("Enter valid integer!"); + } +} diff --git a/DeitelC/Chapter4/a.exe b/DeitelC/Chapter4/a.exe deleted file mode 100644 index 8c66b27..0000000 Binary files a/DeitelC/Chapter4/a.exe and /dev/null differ diff --git a/DeitelC/Chapter4/a.out b/DeitelC/Chapter4/a.out index d707f01..ee38609 100644 Binary files a/DeitelC/Chapter4/a.out and b/DeitelC/Chapter4/a.out differ diff --git a/DeitelC/Chapter4/factorial1to5.c b/DeitelC/Chapter4/factorial1to5.c new file mode 100644 index 0000000..2859491 --- /dev/null +++ b/DeitelC/Chapter4/factorial1to5.c @@ -0,0 +1,11 @@ +#include + +int main(void) { + puts("Factorial 1 to 5: "); + double sum = 1; + for(double i = 1; i <= 20; i++) { + sum *= i; + printf("The %0.lf! are: %0.lf\n", i, sum); + } + return 0; +} diff --git a/DeitelC/Chapter4/trianglePrinting.c b/DeitelC/Chapter4/trianglePrinting.c new file mode 100644 index 0000000..d9a5339 --- /dev/null +++ b/DeitelC/Chapter4/trianglePrinting.c @@ -0,0 +1,11 @@ +#include + +int main(void) { + int start = 10; + for(int i = 1; i <= start; i++) { + for(int j = 0; j <= i; j++){ + printf("%s", "*"); + } + } + printf("\n"); +} diff --git a/a.out b/a.out new file mode 100644 index 0000000..b86930d Binary files /dev/null and b/a.out differ diff --git a/montyHall.c b/montyHall.c new file mode 100644 index 0000000..972097a --- /dev/null +++ b/montyHall.c @@ -0,0 +1,49 @@ +#include +#include +#include + +int main(void) { + srand(time(NULL)); // Seed random number generator + int r, s, openD; + int counter = 0, win = 0; + + while (1) { + r = rand() % 3; // Winning door (0, 1, or 2) + s = -1; + + puts("Select one of the 3 doors (0, 1, 2). Enter -1 to quit:"); + scanf("%d", &s); + if (s == -1) break; + + // Open a door with a goat (not the winning door or the selected door) + openD = rand() % 3; + while (openD == r || openD == s) { + openD = rand() % 3; + } + + printf("You chose: %d, a goat is behind door: %d\n", s, openD); + puts("Do you want to choose another door? (Enter new door number):"); + scanf("%d", &s); + + while (s == openD) { + puts("This door is already opened! Select another door:"); + scanf("%d", &s); + } + + if (s == r) { + puts("YOU WON!"); + win++; + } else { + puts("YOU LOSE!"); + } + counter++; + } + + if (counter > 0) { + printf("Win percentage: %d%%\n", (win * 100) / counter); + } else { + puts("No games played."); + } + + return 0; +}