Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lab-6-RepetitionControlStructures - Part II

The document discusses repetition structures in C programming, including do-while loops, nested loops, and examples of their usage. It provides tasks for students to write programs using these structures, including drawing flowcharts and modifying examples to produce different outputs.

Uploaded by

Salam R. Ammari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab-6-RepetitionControlStructures - Part II

The document discusses repetition structures in C programming, including do-while loops, nested loops, and examples of their usage. It provides tasks for students to write programs using these structures, including drawing flowcharts and modifying examples to produce different outputs.

Uploaded by

Salam R. Ammari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CSE 103: Computer Programming in C

Lab #5: Repetition Structures – Part II


Objective:
Learn the Repetition Structures by covering the following loops:
1. Do-while statement
2. Nested loops circumference

Task 1: do-while statement


Write Compile & Run the following C program in Dev-C++.
Save the source file in a new folder Lab6 as Lab6/program1.c
/* computing the area and circumference of any circle */
#include<stdio.h>
#define PI 3.142857
int main(void){
double radius , area, cir ;
char ans;
do
{
printf("\nEnter a value for radius: ");
scanf("%lf", &radius);
area = PI * radius * radius ;
cir = 2 * PI * radius ;
printf("The area of a circle with radius %.2lf= %.2lf \n", radius , area);
printf("and the circumference = %.2lf \n", cir);
printf("\nDo you wish to try again [y/n]? ");
scanf(" %c", &ans);
} while (ans == 'Y' || ans == 'y');
printf("\nGood bye!!\n");

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:

4. Rewrite Program1.c using while loop.


Save the source file in a new folder Lab6 as Lab6/program2.c
Note: Program2.c must have the same behaviour (semantec) of Program1.c, i.e, the
loop body must be executed at least once!
/* Program1 using while-loop */
#include<stdio.h>
#define PI 3.142857
int main(void){
double radius , area, cir ;
char ans;
………
………
………
………
………
printf("\nGood bye!!\n");
return 0;
}

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;
}

1. What is the Output of the program?

2. Write down the body of the outer loop

3. Write down the body of the inner loop

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
********************
* *
* *

< -- 10 lines -- >


* *
* *
* *
* *
* *
* *
* *
* *
********************
< --- 20 stars --- >

/* Program4: Modified version of Program3


to print Box Rectangle */
#include<stdio.h>
int main(void){
int i , j , rows , columns;
rows = ?? ;
columns = ?? ;
………
………
………
………
………
return 0;
}

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 ;

for(i = 1 ; i < 10 ; i++){


for(j = 1 ; j <= i ; j++)
printf("%d" , j);
printf("\n");
}

return 0;
}
1. What is the Output of the program?

2. Modify Program5 to get the following output


Save the source file in as Lab6/program6.c
1
21
321
4321
54321
654321
7654321
87654321
987654321

/* Program6: Modified version of Program5 */


#include<stdio.h>
int main(void){
int i , j , rows , columns;

………
………
………
………
………
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

/* Program7: Modified version of Program6 */


#include<stdio.h>
int main(void){
int i , j , rows , columns;
………
………
………
………
return 0;
}

4. Modify Program7 to print Pascal Triangle as shown below:


Save the source file in as Lab6/program8.c
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321

/* Program8: Modified version of Program7 to print Pascal


Triangle */
#include<stdio.h>
int main(void){
int i , j , rows , columns;
………
………
………
return 0;
}

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);

printf("Now your input is correct");


getch();

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

You might also like