//program to display a for loop #include<stdio.h> int main(void) { int num; printf(" n n cubed\n"); for(num=1;num<=10;num++) printf("%5d %5d\n", num , num*num*num); return 0; } //program to display a for loop #include<stdio.h> int main(void) { int num;
}
for(num=10;num>0;num--) printf("%d seconds !\n", num); printf("Blast off ! \n"); return 0;
//for loop example //increment an amount 20% #include<stdio.h> int main(void) { double cash; for(cash = 200;cash<=400;cash = cash * 1.2) printf("The amount you owe is $%.2f .\n", cash); }
return 0;
//for loop example //using a printf() statement in a for loop #include<stdio.h> int main(void) { int num; for(printf("Please enter the magic number .\n");num!=6;) scanf("%d",&num); printf("You got it very good \n");
return 0; }
//do while example #include<stdio.h> int main(void) { const int SECRET_CODE = 15; int code; do{ printf("Type the secret code number to enter.\n"); scanf("%d", &code); }while(code!=SECRET_CODE); printf("Well done , you can now enter\n"); return 0; }
/*while example*/ #include<stdio.h> int main(void) { const int SECRET_CODE = 15; int code; printf("Type the secret number to enter.\n"); scanf("%d", &code); while(code != SECRET_CODE) { printf("Type the secret number to enter.\n"); scanf("%d", &code); } printf("Well done , you can now enter.\n"); }
return 0;
/*while example*/ #include<stdio.h>
int main(void) { const int SECRET_CODE = 15; int code; printf("Type the secret number to enter.\n"); scanf("%d", &code); while(code != SECRET_CODE) { printf("Type the secret number to enter.\n"); scanf("%d", &code); } printf("Well done , you can now enter.\n"); return 0; }
//nested for loops #include<stdio.h> int main(void) { const int ROWS = 26; const int COLS = 26; int row; char ch; for(row=0;row
return 0;
//if example #include<stdio.h> int main(void) { const int FAIL = 50; //50% is a fail int grade; int fails = 0; int total = 0;
printf("Please enter your percentages.\n"); printf("Enter q to quit.\n"); while(scanf("%d", &grade)==1) { total++; if(grade < FAIL) fails++; //increment fail count if grade is less than 50 } if(total !=0) printf("%d grades in total: %.1f%% were fails.\n", total , 100.0 * (float) fails / total); if(total == 0) printf("You didnt enter any grades.\n"); }
return 0;