Nested for loop demo
#include <stdio.h>
int main(void)
{
int answer, i, chances, right;
for(i = 1; i < 11; i++) {
printf(" %d + %d = ?", i , i);
scanf("%d", &answer);
if(answer == i + i)
printf("Right!\n");
else {
printf("Wrong.\n");
printf("Try again.\n");
right = 0;
/* nested for */
for(chances = 0; chances < 3 && !right; chances++) {
printf(" %d + %d = ? ", i, i);
scanf("%d", &answer);
if(answer == i + i) {
printf("Right!\n");
right = 1;
}
}
/* if answer still wrong, tell user */
if(!right)
printf("The answer is %d.\n", i + i);
}
}
return 0;
}
Related examples in the same category