0% found this document useful (0 votes)
67 views7 pages

Lab-05 29 March2017

The document discusses nested loops in C programming. It provides examples of different types of nested loops using for, while, and do-while loops. It also includes tasks for readers to write programs to output different patterns using nested loops, and to predict the output of sample programs using loops.

Uploaded by

raja
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)
67 views7 pages

Lab-05 29 March2017

The document discusses nested loops in C programming. It provides examples of different types of nested loops using for, while, and do-while loops. It also includes tasks for readers to write programs to output different patterns using nested loops, and to predict the output of sample programs using loops.

Uploaded by

raja
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/ 7

Lab-05

CIS-104 BSME

What is Nested Loop?  
A nested loop is just a normal loop except that it is inside the body of another loop. A nested loop can be 
formed by placing a for, while or do‐while loop inside the body of another for, while or do‐while loop. 
There is no limit on the combination of nesting i.e. any loop can be used inside any other loop. 
Following  are  some  combination  of  nested  loops.  Usually  the  loop  inside  the  body  of  another  loop  is 
called inner loop while the other loop is termed as outer loop. 
 
Nested Loops (Syntax) 
The syntax for a nested for loop statement in C is as follows: 
for( init; condition; increment )
{
for( init; condition; increment )
{
statement(s);
}
statement(s);
}
The syntax for a nested while loop statement in C programming language is as follows: 
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
The syntax for a nested do...while loop statement in C programming language is as follows: 
do
{
statement(s);
do
{
statement(s);
}while( condition );

}while( condition );

The syntax of mixed types of loop as nested loops 

for( init; condition; increment )


{
while(condition)
{
statement(s);
}
statement(s);
}

CF‐Lab Exercise, developed by Nauman Shamim Page 1


Example‐01 
The following program prints a block of stars using two for loops, try to execute the program  
 
#include<conio.h> 
#include<stdio.h> 
int main ()  Output
{  * * * * *
int i, j;  * * * * *
  * * * * *
for(i=0;i<5;i++){  * * * * *
  for(j=0;j<5;j++)  * * * * *
      printf(" * "); 
  printf("\n"); 
  } 
   getch(); 
return 0; 

Explanation 
There are two loops in the program , the inner loop prints a * five times , the outer loop repeats the inner loop 5 
times and prints a new line in each repetition 
 
Task‐01 
Try to modify the above program such that the program print following pattern 
 
0    0    0    0    0 
 1    1    1    1    1 
 2    2    2   2    2 
 3    3    3   3    3 
 4    4    4    4    4 
 
Task‐02 
If you are able to complete the task‐01 try to modify the develop code and print the following pattern 
 
 0   1    2    3    4 
 0    1    2    3    4 
 0    1    2    3    4 
 0    1    2    3    4 
 0    1    2    3    4 
 
Task‐03 
Modify the code develop in above program and try to print the following pattern, if you cannot do it find the 
solution at the last page of this document 
 
 0 
 0    1 
 0    1    2 
 0    1    2    3 
 0    1    2    3    4 
 
 
 

CF‐Lab Exercise, developed by Nauman Shamim Page 2


Task‐04 
Try to write the program for the following patterns, you may use any types of loop/s 
 
* * * * * * * * * * * * * * *
* * * * * * * * * * * * ** * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
     
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
     
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * *

 
Task‐05 
Try the above patterns using number series as discussed in the class 
 
 
   

CF‐Lab Exercise, developed by Nauman Shamim Page 3


Self‐Assessment Exercise 
Try to predict the output / behavior of the following program, note down the predicted 
output on your notebook , execute the program and compare the two outputs 
 
Program‐01 

#include <stdio.h>
#include <conio.h>
void main()
{
int a=1;
while(a%2!=0){
printf(”\nEnter Value of a = “);
scanf(“%d”,&a);
}
}

Program‐02 

#include <stdio.h>
#include <conio.h>
void main()
{
int a=205,b=450;
int diff=500;
while(diff>10){
b=b-25;
diff=b;
printf(“ a = %d , b=%d”,a,b);
printf(”\nDifference = %d“,diff);
}
}

CF‐Lab Exercise, developed by Nauman Shamim Page 4


Program‐03 
 
#include <stdio.h>
#include <conio.h>
void main()
{
int a=2;
while(a<1024){
a=a*2;
printf(”\nValue of a = %d“,a);
}
}
 
Program‐04 
 
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
for(i=0;i<5;i++){
printf(“ \n i = %d”,i*i);
}
}
Program‐05 
 
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++){
for(i=1;i<=5;i++){
printf(“ \n i x j = %d”,i*j);
}
}
}

CF‐Lab Exercise, developed by Nauman Shamim Page 5


Program‐06 
 
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
for(j=2;j<=15;j++){
for(i=210;i<=230;i++){
if(i%j==0)
printf(“ \n %d divides %d completely”,j,i);
}
}
}

Program‐07 
 
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
int limit;

for(j=2;j<5;j++){
limit=j*j;
printf(“\nNumbers between 0 and %d are “,limit);
for(i=1;i<j*j;i++){
printf(“\n %d ”,i);
}
printf(“\n************************************************”);
}
printf(“End of Program”);

 

CF‐Lab Exercise, developed by Nauman Shamim Page 6


Solutions

Task-03

#include<conio.h> 
#include<stdio.h> 
int main () 

int i, j; 
 
for(i=0;i<5;i++){ 
  for(j=0;j<=i;j++) 
      printf(" %d ",j); 
  printf("\n"); 
  } 
   getch(); 
return 0; 

CF‐Lab Exercise, developed by Nauman Shamim Page 7

You might also like