Lab-6-RepetitionControlStructures - Part II
Lab-6-RepetitionControlStructures - Part II
return 0;
}
1. Draw the flowchart of the program
Use pen and paper to draw the flowchart then take a photo and attach it here
Page 1 of 9
2. Give a sample Output of the program
3. Run the Debugger on the program several times and write down your observations
regarding do-while statement.
Observations:
Conclusion:
We can use while/for/do-while statements interchangeably provided that we make the
necessary changes to our program!
Page 2 of 9
Task 2: Nested for loops:
Write Compile & Run the following C program in Dev-C++.
Save the source file in as Lab6/program3.c
Compile & Run the C program in Dev-C++.
#include<stdio.h>
int main(void){
int i , j , rows , columns;
rows = 9 ;
columns = 9 ;
for(i = 1 ; i <= rows ; i++){
for(j = 1 ; j <= columns ; j++)
printf("*");
printf("\n");
}
return 0;
}
4. Run the program with different values for rows , columns and notice the output!
Page 3 of 9
5. Modify the program to get the following output
6. Save the source file in as Lab6/program4.c
********************
* *
* *
Page 4 of 9
Task 3: Nested for loops:
Write Compile & Run the following C program in Dev-C++.
Save the source file in as Lab6/program5.c
Compile & Run the C program in Dev-C++.
#include<stdio.h>
int main(void){
int i , j ;
return 0;
}
1. What is the Output of the program?
………
………
………
………
………
return 0;
}
Page 5 of 9
3. Modify Program6 to get the following output
Save the source file in as Lab6/program7.c
1 x 1
12 x 21
123 x 321
1234 x 4321
12345 x 54321
123456 x 654321
1234567 x 7654321
12345678 x 87654321
123456789 x 987654321
Page 6 of 9
do-while Statement
Do-while can be used in a menu driven program. The example shown below will continue
running as long as the user did not enter the number 5.
#include <stdio.h> // EXAMPLE-3
#include <conio.h>
void menu();
int main()
{
int choice;
do{
menu();
printf("Enter your choice >");
scanf("%d",&choice);
// Here come the statements to do
// the different tasks
} while (choice != 5);
getch();
}
void menu ()
{
printf("1-addition \n");
printf("2-subtraction\n");
printf("3-multiplication\n");
printf("4-division\n");
printf("5-Exit\n");
}
Do-while can also be used to validate an input as indicated in the example below.
#include <stdio.h> // EXAMPLE-4
#include <conio.h>
int main()
{
int n;
do{
printf ("Enter an integer number in [10,100] interval
>");
scanf("%d",&n);
if(n<10 || n>100)
printf("Sorry wrong input, try again\n");
}while (n<10 || n>100);
Page 7 of 9
return 0;
}
Page 8 of 9
Nested Loops:
We can have one or more loops defined inside another loop. These are called nested loops. The
example shown below uses a nested loop to compute the sum of integer numbers from 1 to
10.
#include <stdio.h> // EXAMPLE-5
#include <conio.h>
int main()
{
int sum,i,j;
for(i=1;i<=10;i++)
{
sum=1;
printf("1");
for(j=2;j<=i;j++)
{
sum=sum+j;
printf("+%d",j);
}
printf("=%d\n",sum);
}
getch();
return 0;
}
Page 9 of 9