Lab 6&7 Loops
Lab 6&7 Loops
______________________
Course Instructor / Lab Engineer
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
Page 4
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Page 5
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
printf("\n%d",i); 9
i++; 10
}
getch();
}
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
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
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.
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 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;
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
Page 16
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
2222
11111
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