#include<stdio.h>
main()
{
int i,j,x,y;
int div=0;
printf("Enter two nos\n");
scanf("%d%d",&x,&y);
for(i=x;i<=y;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
div=div+1;
}
if(div==2)
printf("%d ",i);
div=0;
}
}
ASSIGNMENT NO 5)
TO DEMONSTRATE USE OF NESTED LOOPS
In the previous exercise, you used while, do-while and for loops. You should read following topics before starting this exercise.
1) Different types of loop structures in C.
2) Syntax for these statements.
3) Usage of each loop structure.
Nested loop means a loop that is contained within another loop. Nesting can be done upto any levels. However the inner loop has to be completely enclosed in the outer loop. No overlapping of loops is allowed.
i) Nested for loop
Syntax= for(inilz;condn;incr/dcr)
{
for(inilz;condn;incr/dcr)
{
statements;
}
}
ii) Nested while loop/ do- while loop
Syntax= while(conditions1)
{
while(condition 2)
{
stmts;
}
stmts;
}
do
{
while(condition1)
{
stmts;
}
stmts;
} while(condition2)
EXAMPLES OF DO-WHILE & FOR LOOPS
main()
{
int i,j,x,y;
int div=0;
printf("Enter two nos\n");
scanf("%d%d",&x,&y);
for(i=x;i<=y;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
div=div+1;
}
if(div==2)
printf("%d ",i);
div=0;
}
}
ASSIGNMENT NO 5)
TO DEMONSTRATE USE OF NESTED LOOPS
In the previous exercise, you used while, do-while and for loops. You should read following topics before starting this exercise.
1) Different types of loop structures in C.
2) Syntax for these statements.
3) Usage of each loop structure.
Nested loop means a loop that is contained within another loop. Nesting can be done upto any levels. However the inner loop has to be completely enclosed in the outer loop. No overlapping of loops is allowed.
i) Nested for loop
Syntax= for(inilz;condn;incr/dcr)
{
for(inilz;condn;incr/dcr)
{
statements;
}
}
ii) Nested while loop/ do- while loop
Syntax= while(conditions1)
{
while(condition 2)
{
stmts;
}
stmts;
}
do
{
while(condition1)
{
stmts;
}
stmts;
} while(condition2)
EXAMPLES OF DO-WHILE & FOR LOOPS
* ** *** **** *****
#include <stdio.h> int main() { int i=1,j; do { j=1; do { printf("*"); j++; }while(j <= i); i++; printf("\n"); }while(i <= 5); return 0; }
In this program, nested do-while loop is used to print the star pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only one "*" is printed, then on the next loop it's 2 printing two stars and so on till 5 iterations of the loop executes, printing five stars. This way, the given star pattern is printed.
Nested for loop
A for loop inside another for loop is called nested for loop.
Syntax of Nested for loop
for (initialization; condition; increment/decrement) { statement(s); for (initialization; condition; increment/decrement) { statement(s); ... ... ... } ... ... ... }
Flowchart of Nested for loop

Example of Nested for loop
C program to print all the composite numbers from 2 to a certain number entered by user.
#include<stdio.h> #include<math.h> int main() { int i,j,n; printf("Enter a number:"); scanf("%d",&n); for(i=2;i<=n;i++) { for(j=2;j<=(int)pow(i,0.5);j++) { if(i%j==0) { printf("%d is composite\n",i); break; } } } return 0; }
Output
Enter a number:15 4 is composite 6 is composite 8 is composite 9 is composite 10 is composite 12 is composite 14 is composite 15 is composite
A number is said to be composite if it has at least one factor other than 1 and itself. This program prints all the composite numbers starting from 2 to a certain number n, entered by user. We need to use a nested loop to solve this problem. The outer for loop runs from 2 to n and the inner loop is used to determine whether a number is composite or not. We need to check for that factor starting from 2 to integer part of square root of that number.
Consider 15, its square root is nearly 3.873. Here, the integer part is 3. Now, if there is a factor of 15 from 2 to 3 then it is composite. Here, 3 is a factor of 15. Hence, 15 is a composite number.
No comments:
Post a Comment