Professional Documents
Culture Documents
Assginment 1
Assginment 1
What is Loop? It is the ability to perform a set of instructions repeatedly. This involves repeating
some portion of the program either a specified number of times or until a particular condition is
being satisfied. This repetitive operation is done through a loop control instruction. There are
three methods by way of which we can repeat a part of a program. Using a for statement, while
statement or do-while statement.
Nesting of loops is the feature that allows the looping of statements inside
another loop. Any number of loops can be defined inside another loop, i.e., there
is no restriction for defining any number of loops. The nesting level can be
defined at n times.
The nested for loop means any type of loop which is defined inside the 'for' loop.
#include <stdio.h>
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) {
for(int j=1;j<=10;j++) {
printf("%d\t",(i*j));
printf("\n");
return 0;
}
Nested while loop
The nested while loop means any type of loop which is defined inside the
'while' loop.
#include <stdio.h>
int main()
{
int rows,columns,k=1;
printf("Enter the number of rows :");
scanf("%d",&rows);
printf("\nEnter the number of columns :");
scanf("%d",&columns);
int a[rows][columns],i=1,j=1;
while(i<=rows)
{
while(j<=columns)
{
printf("%d\t",k);
k++;
j++;
}
i++;
printf("\n");
}
}
OUTPUT:
Nested do..while loop
The nested do..while loop means any type of loop which is defined inside the
'do..while' loop.
#include <stdio.h>
int main()
{
int i=1,j=1;
do
{
int j=1;
do
{
printf("*");
j++;
}while(j<=8);
printf("\n");
i++;
}while(i<=4);
}
OUTPUT:
Exercise 2.2:
wizard(ron).
hasWand(harry).
quidditchPlayer(harry).
hasBroom(X):- quidditchPlayer(X).
1. wizard(ron).--------------true.
3. wizard(hermione).---------------false.
5. wizard(harry).---------------true
6. wizard(Y).-----------Y=RON
7. witch(Y).------------DOESNOT EXIST
Exercise 2.1:
1. Butch is a killer.killer(butch).
3. Zed is dead.dead(zed).
#include <stdio.h>
int main() {
int r, f = 1, s, i, j;
scanf("%d", &r);
printf(" ");
if (j == 0 || i == 0)
f = 1;
else
f = f * (i - j + 1) / j;
printf("%4d", f); }
printf("\n");
return 0; }