-
Notifications
You must be signed in to change notification settings - Fork 3
/
ass6_20CS30042_test4.c
62 lines (55 loc) · 1.43 KB
/
ass6_20CS30042_test4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
int printStr(char *s);
int readInt(int *eP);
int printInt(int n);
int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
int main() {
printStr("\n#### TEST 4 (Loops) ####\n");
printStr("\nPattern printing using FOR loop:");
printStr("\n\n");
int n = 9;
int i, j;
for(i = 1; i <= 2 * n - 1; i++) {
int x1 = n + 1 - i;
int x2 = i - n + 1;
for(j = 1; j <= 2 * n - 1; j++) {
int y1 = n + 1 - j;
int y2 = j - n + 1;
printInt(max(max(x1, x2), max(y1, y2))); // nested function call check
printStr(" ");
}
printStr("\n");
}
printStr("\n");
int pow2 = 1;
printStr("\nPowers of 2 using WHILE loop: ");
while(pow2 <= 1024) {
printInt(pow2);
printStr(" ");
pow2 = pow2 * 2;
}
printStr("\n\n");
i = 0;
int iters, ep;
printStr("\nTesting DO-WHILE loop:");
do {
if(i == 0) {
printStr("\nEntered do-while loop. Enter number of times you wish to run the loop after this: ");
ep = readInt(&iters);
if(ep == 0) {
printStr("\nInvalid input. Exiting...\n\n");
return 0;
}
} else {
printStr("\nIteration ");
printInt(i);
printStr("\n");
}
} while(i++ < iters);
printStr("\n\n");
return 0;
}