Professional Documents
Culture Documents
Iterative Statements
Iterative Statements
-Iterative statements (loops) allow set of instructions to be executed or performed several times until certain conditions are met. -It can be predefined as in the for loop, or openended as in while and do-while.
Note:
Never place a semi-colon right after the for header. This is a logical error. Never change the value of the for loops counter inside the body of the loop. This affect the result of the program. The increment part of the for loop is execute after the first iteration of the loop.
Write a program that will print the numbers 1 to 10 using a for statement
#include <stdio.h> int x; main() { for (x=1; x<=10; x++) printf(%d\n,x); getch(); }
Output
1 2 3 4 5 6 7 8 9 10
Write a program that will get the sum of all integers from 1 to 10.
#include<stdio.h> int x, sum; main() { sum = 0; for (x=1; x<=10; x++) sum = sum + x; printf(The sum of 1 to 10 is %d\n, sum); getch(); }
Output
The sum of 1 to 10 is 55
Write a program that will print the numbers 1 to 10 using while statement.
#include<stdio.h> int x; main() { x=1; while(x<=10) { printf(%d\n,x); x++; } getch(); }
Output
1 2 3 4 5 6 7 8 9 10
Write a program that will get the average of all integers from 1 to 10 using do-while loop
#include<stdio.h> int x, sum; float average; main() { sum = 0; x=1; do { sum=sum + x; x++; }while (x<=10) average = sum/10.00; printf(The computed average is %.2f\n, average); getch(); }
Output
The computed average is 5.50
Example
Design a program that calculates the sum of the input given number of n using three looping statements.
Design a program that calculates the sum of the input given number of n using three looping statements.
#include<stdio.h> main(){ int m,e,sum; clrscr(); printf(Enter your desire looping number:); scanf(%d,&m); sum=0; for(e=1;e<=m;e++){ printf(\n%d,e); sum=sum+e;} printf(\n The sum:%d,sum); getche(); }
DO WHILE LOOP
#include<stdio.h> main(){ int m,e,sum; clrscr; printf(Enter your desire looping number:); scanf(%d,&m); e=1; sum=0; do{ printf(\n%d,e); sum=sum+e; e++; }while(e<=m); printf(\n The sum:%d,sum); getche(); }
while
#include<stdio.h> main(){ int m,e,sum; clrscr; printf(Enter your desire looping number:); scanf(%d,&m); e=1; sum=0; while(e<=m){ printf(\n%d,e); sum=sum+e; e++; printf(\n The sum:%d,sum); getche(); }
Design a program that displays the Fibonacci sequence numbers of n which is display number by a user. Apply three solutions using the three looping statements.
TRY
Design a program that outputs the given whole number using three looping statements. Hint: use decrement.
DO WHILE LOOP
#include<stdio.h> main(){ int m; clrscr(); printf(Example number two looping statements:); m=8; do{ printf(\n%d,m); m--; delay(5000); }while(m>=1); getche();}
WHILE loop
#include<stdio.h> main(){ int m; clrscr(); printf(Example number two looping statements:); m=8; while(m>=1){ printf(\n%d,m); delay(5000); m--;} getche(); }
TRY AGAIN
Design a program that reverses the input number of n. Formulate an equation to come up with the answer.
#include<stdio.h> main(){ int m,e; clrscr(); print(\t\t\tAn example of reversed number\n\n); printf(\nEnter a value:); scanf(%d,&m); for(;m!=0;){ e=m%10; printf(%d,e); m=m/10; } delay(50000); getch(); }