Chapter4
This commit is contained in:
parent
0b7e3b6cb0
commit
08ec3be7a9
26
DeitelC/Chapter4/NaturalNumberArithmetic.c
Normal file
26
DeitelC/Chapter4/NaturalNumberArithmetic.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
11
DeitelC/Chapter4/factorial1to5.c
Normal file
11
DeitelC/Chapter4/factorial1to5.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
11
DeitelC/Chapter4/trianglePrinting.c
Normal file
11
DeitelC/Chapter4/trianglePrinting.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int start = 10;
|
||||
for(int i = 1; i <= start; i++) {
|
||||
for(int j = 0; j <= i; j++){
|
||||
printf("%s", "*");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
49
montyHall.c
Normal file
49
montyHall.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user