0% found this document useful (0 votes)
2 views17 pages

Lab 6&7 Loops

The document is a lab manual for a Programming with C course at Fayoum International Technological University, focusing on control statements and looping structures. It covers various types of loops such as while, do...while, and for loops, along with examples and explanations of their syntax and usage. Additionally, it discusses control statements like break and continue, providing practical examples for better understanding.

Uploaded by

hossamarpro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Lab 6&7 Loops

The document is a lab manual for a Programming with C course at Fayoum International Technological University, focusing on control statements and looping structures. It covers various types of loops such as while, do...while, and for loops, along with examples and explanations of their syntax and usage. Additionally, it discusses control statements like break and continue, providing practical examples for better understanding.

Uploaded by

hossamarpro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming with C - Lab

Fayoum International Technological University

Student Name: ……………………………………………………………….…………


Student Group: …………………………………………………………………………

Lab Instructor : Abdullah Emam

Lab Performed In: School Lab

Programming with C - Lab


Semester - II

Fayoum International Technological University


Faculty of Technology
Information Technology Program
First Year – Essentials in C programming
Academic Year (2024/2025)

______________________
Course Instructor / Lab Engineer

Prepared by Abdullah Emam

Page 1
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

UNIT-2
WEEK - 5
1 Control Statements (Looping): While,
2 Do..While, For Loop, Continue & Break (Unconditional)
3 Nested Loops,
4 Sample C Programs

Page 2
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

Looping

Loop is a mechanism through which you repeatedly execute a set of statements. In


looping, a sequence of statements is executed until some condition for termination of the
loop is satisfied.
A program loop therefore consists of two segments, one the body of the loop & other the
control statement. The control statement tests certain conditions and then directs the
repeated execution of statements contained in body of the loop.
Depending on the position of the control statement in the loop, a control structure may be
classified either as entry-controlled loop or exit-controlled loop.
□ In entry-controlled loop, the control conditions are tested before start of the execution. If
conditions are not satisfied, then body of the loop will not be executed.
□ In exit-controlled loop, the test is performed at the end of the body of the loop and
therefore the body is executed unconditionally for the first time.

Loop Control Structures


The test conditions should be carefully stated in order to perform the desired number of
loop executions. It is assumed that the test condition will eventually transfer the control
out of the loop. In case, due to some reason it does not do so, the control sets up an
infinite loop and the body is executed over and over again.
A looping process, in general, would include the following four steps:
1. Setting & Initialization of a counter
2. Execution of the statements in the loop
3. Test for a specified condition for execution of the loop
4. Updating the counter
The test may be either to determine whether the loop has been repeated the specified
number of times or to determine whether a particular condition has been met.
Page 3
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
The C language provides the following loop constructs:
(a) while statement
(b) do..while statement
(c) for statement

Page 4
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

The WHILE Statement


□ The simplest of all looping structures is the while (an entry-controlled loop) statement.
□ The basic format of while statement is
Syntax:
while(test_condition)
{
statement;
}
test_condition Is any valid C condition. Statement is repeatedly executed as long
as condition is true. Once the condition is false, loop is
terminated and control is transferred to the statement that is
immediately after the loop.
statement (Body of the loop) May be either a single statement or a
compound statement.
□ On exit, the program continues with the statement immediately after the body of
the loop.
Example 1: To print the message 5 times
/*Print message 10 times*/
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
while(i<=5)
{ Output:
printf("\nCPNM"); CPNM
i++; CPNM
} CPNM
} CPNM
CPNM
Example 2: To print 1 to 10 numbers.

/*Print 1 to 10 numbers*/ Output:


#include<stdio.h> 1
#include<conio.h> 2
main() 3
{ 4
int i=1; 5
clrscr(); 6
while(i<=10) 7
{ 8

Page 5
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
printf("\n%d",i); 9
i++; 10
}
getch();
}

Example 3: To print 1 to 10 odd numbers.


/*Print 1 to 10 numbers*/
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;

while(i<=10) { Output:
printf("\n%d",i); 1
i+ = 2; 3
} 5
7
} 9
Example 4: To print largest of the given numbers.
/*Program to display the largest of given numbers*/
#include<stdio.h>
main()
{
int num;
int largenum=0;
printf("Enter a number (0 to stop): ");
scanf("%d",&num);
while(num!=0)
{
if(num>largenum)
largenum = num;
printf("Enter a number (0 to stop): ");
scanf("%d",&num);
}
printf("Largest number is %d",largenum);
}
Output:
Enter a number (0 to stop): 6
Enter a number (0 to stop): 9
Enter a number (0 to stop): 16

Page 6
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Enter a number (0 to stop): 98
Enter a number (0 to stop): 0
Largest number is 98
The DO…WHILE Statement
□ When you need to execute statements at least for once irrespective of the result of
the condition then you have to use do...while loop.
□ Unlike while loop, in which condition is checked at the top of the loop; in
do...while, condition is checked at the bottom.
□ Do…while executes statements first and then checks condition. As the result
statements are executed at least once as condition is not at all checked before the
first iteration.
□ The basic form of do…while is
Syntax:
do
{
statements;
}
while(test_condition);

Braces { } are must for do…while. And we can have any number of statements between
braces (body of the loop).
□ Since the test_condition is evaluated at bottom of the loop, the do…while construct
provides an exit-controlled loop and therefore the body of loop is always executed
at least once.
Example 1: To calculate sum of 10 natural numbers.
/*Calculate sum of 10 natural numbers*/
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,sum=0;
do
{
sum + = i;
i++;
}
while(i<=10);
printf("\nThe sum of 10 natural numbers is: %d",sum);
}

Page 7
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Output:
The sum of 10 natural numbers is: 55

Example 2: To print Multiplication table.


/*Program to print Multiplication Table*/
#include<stdio.h>
#define COLMAX 10
#define ROWMAX 10
main()
{
int row,column,y;
row=1;
printf(" MULTIPLICATION TABLE\n");
printf(" \n");
do
{
column=1;
do
{
y=row*column;
printf("%4d",y);
column = column+1;
}
while(column<=COLMAX);
printf("\n");
row = row+1;
}
while(row<=ROWMAX);
printf(" \n");
}
Output:
MULTIPLICATION TABLE

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Prepared by IT & CSE Page 8


Programming with C - Lab

FOR Loop:
□ This is another entry control loop.
□ This integrates 3 basic ingredients of a loop (initialization, condition and
incrementing).
□ For loop is typically used to repeat statements for a fixed number of times.
□ The basic form of for statement:
Syntax:
for(initialization;condition;updation)
{
statement;
}

initialization Executed only for once just before loop starts. Normally counter
(variable used in loop) is initialized here.
condition Is any valid C condition. As long as this is true statement is
repeatedly executed.

updation
Executed after the statement is executed. Typically contains incrementing counter or decrementing
counter as the case may be.
statement (Body of the loop) This is repeatedly executed as long as condition is true. It may be a
compound statement also.

Lab Manual for Programming in C Lab by. Abdullah Emam Page 9


Programming with C - Lab
Note: All the above are optional. So any portion of the loop can be omitted. Though a
portion is omitted, semicolon (;) after that must be given.
The following is an example to display numbers from 1 to 10 using for loop.
for(n=1;n<=10;n++)
printf("%d\n",n);

This is in effect same as while loop used previously. But, as it combines initialization,
condition and updation, it is more easier compared with while loop.

The following is another example for for loop where initialization and updation parts are
omitted.
/*this is to be terminated when 0 or negative number is given*/
for(;n>0;)
scanf("%d",&n);

Note: In C language, for loop can be used in place of while loop and vice-versa. Both of them
execute statements as long as the condition is true and terminate the loop once condition is
false.
The for statement
for(; ;)
{
statement;
}
is an infinite loop. This can be terminated using a break statement or an exit() function.

Comma Operator
It is possible to have more than one expression in initialization and updation portions
using (comma) operator. Comma operator is used to separate expressions.
The following is an example of for loop using comma operator:
printf("\nn\tj");
for(n=0,j=10;n<j;n++,j--)
printf("\n%d\t%d",n,j);
The output of the above example is
n j
0 10
1 9
2 8
3 7

Page 10
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
4 6

Example 1: To calculate factorial of any number


/*Program to calculate factorial of given number*/
#include<stdio.h>
main()
{
int i,n;
long int fact=1;
clrscr();
printf("Enter n value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d is %ld",n,fact);
getch();
}
Output:
Enter n value: 8
Factorial of 8 is 40320

Example 2: Program to find number of even and odd numbers in the list
/*Program to find number of even and odd numbers in the list*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,num,ecount=0,ocount=0;
printf("Enter number of values: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter a value: ");
scanf("%d",&num);
if(num%2==0) ecount++;
else ocount++;
}
printf("Even count in the list of %d numbers is %d\n",n,ecount);
printf("Odd count in the list of %d numbers is %d\n",n,ocount);
}
Output:
Enter number of values: 10

Page 11
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Enter a value: 8
Enter a value: 6
Enter a value: 4
Enter a value: 12
Enter a value: 9
Enter a value: 2
Enter a value: 3
Enter a value: 1
Enter a value: 15
Enter a value: 98
Even count in the list of 10 numbers is 6
Odd count in the list of 10 numbers is 4

break statement
This is used to terminate a loop. A loop can be terminated either when condition is false
or when you execute break statement. When you have to terminate loop based on some
other condition other than condition of the loop then you can use break statement.
Example 1: Program to implement break statement
/*Program to Implement break Statement*/
#include<stdio.h>
main()
{
int i;
for(i=1; i<=10; i++)
{
printf("\n%d", i); if (i == 7)
break;
}

}
Output:
1
2
3
4
5
6
7

Page 12
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

Example 2: Program to display sum of 10 numbers or till 0 is given which comes first.
/*Program to display sum of given numbers*/
#include<stdio.h>
main()
{
int sum=0,n,c;
for(c=1;c<=10;c++)
{
printf("Enter a number (0 to stop): ");
scanf("%d",&n);
if(n==0)
break;
sum += n;
}
printf("Sum = %d",sum);
}
Output:
Enter a number (0 to stop): 5
Enter a number (0 to stop): 6
Enter a number (0 to stop): 4
Enter a number (0 to stop): 2
Enter a number (0 to stop): 8
Enter a number (0 to stop): 0
Sum = 25

Page 13
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

continue statement
This is used to transfer control to the beginning of the loop from within the loop. It is used
to skip the statements after continue statement and enter into next iteration of the loop.
Example: Program to Implement continue Statement
/*Program finds square of positive numbers only*/
#include <stdio.h>
main()
{
int i, n, a, sq;

printf("\nHow many numbers you want to enter: ");


scanf("%d", &n);
for (i=1;i<=n; i++)
{
printf("\nEnter number: ");
scanf("%d", &a);
if(a<0)
continue;
sq = a * a;
printf("\nSquare = %d\n", sq);
}
}
Output:
How many numbers you want to enter: 3

Enter number: 2

Square = 4

Enter number: -1

Enter number: 6

Square = 36

Nested Loops
When a loop is placed inside another loop, it is called as nested loop. C allows nested
loops. The inner loop or nested loop is executed for each repetition of the outer loop.
Example 1: Display numbers in the following format.
1 2 3 4 5
1 2 3 4 5

Page 14
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
#include<s
tdio.h>
main()
{
int
i,j;
r();
for ( i = 1; i <= 5 ; i++)
{
printf("\n");
for ( j = 1 ; j <= 5 ; j
++) printf("%5d",
j);
}
}
Example 2: Display numbers in the following format.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<s
tdio.h>
main()
{
int
i,j;
r();
for ( i = 1; i <= 5 ; i++)
{
for ( j = 1 ; j <= i ; j ++) {
printf("%5d", j);
}
printf("\n");
}
}

LAB EXERCISE

Page 15
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab

Sheet
1. Write a program to print first 10 even numbers using while loop.
2. Write a program to display sum of first 10 natural numbers.
3. Write a program to find the factorial of a number.
4. Write a program to print sum of first ten even numbers.
5. Write a program to print table of any number entered by user upto n number of times.
Example: Table of : 7 Upto how many times : 15 Then the table of 7 will be displayed
upto 7 × 15 = 105 In this loop will be executed according to the number entered by user
and that’s why the condition is checked at last and the value is entered by user
(Do..While)
6. Write a program to print natural numbers upto n times. (Do..While)
7. Write a program to display the following pattern.
*
**
***
****
*****
8. Write a program to display the following pattern.
1
12
123
1234
12345
9.. Write a program to display the following pattern.
1
22
333
4444
55555

Display numbers in the following format.


11111
2222
333
44
5
Display numbers in the following format.
54321
5432
543
54
5

Display numbers in the following format.


5
44
333

Page 16
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
2222
11111

Display numbers in the following format


*
**
***
* * *
* * * *
* * * * *

Write the code in c for this algorithm to check whether given number is prime number or not.
Step 1: Begin
Step 2: Display "Enter a number: "
Step 3: Read n
Step 4: Initialize c to 0
Step 5: For i = 1 to n, do
Step 5.1: If "n%i==0"
Step 5.1.1: Increment c by 1
Step 5.2: EndIf;
Step 5.3: Increment i by 1
Step 6: EndFor;
Step 7: If "c<=2"
Step 7.1: Display n " is prime number"
Step 8: Else
Step 8.1: Display n " is not prime number"
Step 9: EndIf;
Step 10: End.

Page 17
Lab Manual for Programming in C Lab by. Abdullah Emam

You might also like