132 lines
2.2 KiB
C
132 lines
2.2 KiB
C
|
|
/* lab5.c practice loop and branch
|
|
<Your Name> working with <Your Partner>
|
|
*/
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
|
|
void greetMe();
|
|
void allOdds();
|
|
void allEvens();
|
|
void allThrees();
|
|
void fizzBuzz();
|
|
|
|
int main()
|
|
{
|
|
char select, rerun;
|
|
|
|
while (1) {
|
|
printf("Select one program:\n");
|
|
printf("- A: greetMe\n");
|
|
printf("- B: allOdds\n");
|
|
printf("- C: allEvens\n");
|
|
printf("- D: allThrees\n");
|
|
printf("- E: fizzBuzz\n");
|
|
printf("Enter selction: ");
|
|
scanf("%c", &select);
|
|
|
|
select = toupper(select);
|
|
|
|
switch (select) {
|
|
case 'A':
|
|
greetMe();
|
|
break;
|
|
case 'B':
|
|
allOdds();
|
|
break;
|
|
case 'C':
|
|
allEvens();
|
|
break;
|
|
case 'D':
|
|
allThrees();
|
|
break;
|
|
case 'E':
|
|
fizzBuzz();
|
|
break;
|
|
}
|
|
while (getchar() != '\n');
|
|
|
|
printf("\nRun another? (y/n): ");
|
|
scanf("%c", &rerun);
|
|
|
|
rerun = tolower(rerun);
|
|
|
|
if (rerun == 'n') {
|
|
break;
|
|
}
|
|
while (getchar() != '\n');
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void greetMe(){
|
|
printf("This function will show \"Welcome!\" many times as you request.\n");
|
|
|
|
int times;
|
|
|
|
printf("Enter the number of times you want to show welcome: \n");
|
|
scanf("%d", ×);
|
|
|
|
for (int i = 0; i < times; i++) {
|
|
printf("Welcome!\n");
|
|
}
|
|
|
|
|
|
}
|
|
void allOdds(){
|
|
printf("\nThis function print all odds in [0, 10]\n");
|
|
|
|
|
|
for (int i = 0; i <= 10; i++) {
|
|
if (i % 2) {
|
|
printf("%d\n", i);
|
|
}
|
|
}
|
|
|
|
}
|
|
void allEvens(){
|
|
printf("\nThis function print all evens in [0, 10]\n");
|
|
|
|
for (int i = 0; i <= 10; i++) {
|
|
if (!(i % 2)) {
|
|
printf("%d\n", i);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void allThrees(){
|
|
printf("\nThis function print all mutiple of 3 in [0, 10]\n");
|
|
|
|
for (int i = 0; i <=10; i++) {
|
|
if (!(i % 3)) {
|
|
printf("%d\n", i);
|
|
}
|
|
}
|
|
|
|
}
|
|
void fizzBuzz(){
|
|
printf("\nThis function print numbers 1 to 30, but 'fizz' for multiples of 3, 'buzz' for multiples \
|
|
of 5 and 'fizzbuzz' for multiples of both 3 and 5.\n");
|
|
|
|
for (int i = 1; i <= 30; i++) {
|
|
|
|
if (!(i % 3) && !(i % 5) ) {
|
|
printf("fizzbuzz\n");
|
|
} else if (!(i % 3) ) {
|
|
printf("fizz\n");
|
|
} else if (!(i % 5) ) {
|
|
printf("buzz\n");
|
|
} else {
|
|
printf("%d\n", i);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|