Chapter 11: Iterative Statement
Chapter 11: Iterative Statement
Statement
while statement
The while statement is used when the program needs to perform
repetitive tasks. The general form of a while statement is:
while ( condition)
statement ;
The program will repeatedly execute the statement inside the while until the condition becomes false(0).
(If the condition is initially false, the statement will not be executed.) Consider the following program:
main( )
{ int p, n, count; float r, si;
count = 1;
while ( count <= 3 )
{
printf ( "\nEnter values of p, n and r " ) ; scanf(“%d %d %f", &p, &n, &r ) ;
si=p * n * r / 100 ;
printf ( "Simple interest = Rs. %f", si ) ; count = count+1;
}
}
Some outputs of this program:
Enter values of p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values of p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values of p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
Seatwork
Do a program checks whether a given number is a palindrome or not
Output:
Enter a number to check if it is a palindrome or not 12321
12321 is a palindrome
1 1
2 8
3 27
4 64
5 125
6 216
int main(void)
{
int num;
printf(" n n cubed\n");
for (num = 1; num <= 6; num++)
}while( condition );
Seatwork
program using a nested for loop to find the prime numbers from 2 to 20:
Output
1 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
Seatwork
*
***
*****
*******
*********
Seatwork
• Program to print series from 10 to 1 using nested loops.
Output:
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
End of Module 1