Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, August 23, 2022

Nested loop in C programming

 Nested loop in C programming

The loop inside another loop is called a nested loop. It is a sequence of loop statements used together. The loop statement which is used outside of another is called the outer loop and the loop statement which is used inside of another is called the inner loop statement.

The guidelines for nested loops are:

  • An inner loop and outer loop cannot have the same variable.
  • The inner loop must be nested inside the outer loop.
  • The inner loop must be terminating before the outer loop.
  • The outer loop should be opened first but should be closed last.
  • Loops must never cross each other.


Nested for loop


Syntax:

for (initialization; condition; increment/decrement)

{

    statement(s);

    for (initialization; condition; increment/decrement)

    {

        statement(s);

        ... ... ...

    }

    ... ... ...

}

 

 

Example:

WAP to display the following numeric pattern

1

12

123

1234

12345

#include <stdio.h>

#include <conio.h>

main()

{

int x,y;

for (x=1;x<=5;x++)

{

for (y=1;y<=x;y++)

{

printf("%d",y);

}

printf("\n");

}

getch();

}

 

Nested while and do while loop

While nested loop

Do.. while nested loop

Syntax:

while (condition1)

{

    statement(s);

    while (condition2)

    {

        statement(s);

        ... ....... ...

    }

    ... ... ...

}

 

Syntax:

 

do

{

    statement(s);

    do

    {

        statement(s);

        ... ... ...

    }while (condition2);

    ... ... ...

}while (condition1);

 

Example:

#include<stdio.h>

#include<conio.h>

main()

{

int a=1;

while (a<=5)

{

int b=1;

while(b<=a)

{

printf("%d",b);

b++;

}

printf("\n");

a++;

}

getch();

}

Output:

1

12

123

1234

12345

Example:

 

#include<stdio.h>

#include<conio.h>

{

int a=1;

do

{

int b=1;

do

{

printf("%d",b);

b++;

}while (b<=a);

a++;

printf("\n");

}while(a<=5);

getch();

}

Output:

1

12

123

1234

12345

 

Some solved nested loop programs

1.       WAP to display the following numeric pattern.

            12345

            1234

            123

            12

            1

Ans:

#include<stdio.h>

#include<conio.h>

main()

{

int a=5;

 

while (a>=1)

{

int b=1;

while(b<=a)

{

printf("%d",b);

b++;

}

printf("\n");

a--;

}

getch();

}

2.  Write a program to generate the following:

1

22

333

4444

55555

 

Ans:

#include<stdio.h>

#include<conio.h>

main()

{

int i,j;

for(i=1;i<=5;++i)

{

for(j=1;j<=i;++j)

{

printf("%d",i);

}

printf("\n");

}

getch();

}

3.  WAP to display the following:

55555

4444

333

22

1

 

Ans:

#include<stdio.h>

#include<conio.h>

main()

{

int i,j;

for(i=5;i>=1;--i)

{

for(j=1;j<=i;++j)

{

printf("%d",i);

}

printf("\n");

}

getch();

}

4.         WAP to display the following table:

10        20        30        40        50

20        30        40        50        60

30        40        50        60        70

40        50        60        70        80

50        60        70        80        90

60        70        80        90        100

 

Ans:

#include<conio.h>

#include<stdio.h>

main()

{

int x,y;

for (x=0;x<=5;x++)

{

for (y=1;y<=5;y++)

{

printf("%d\t",(x+y)*10);

}

printf("\n");

}

getch();

}

5. WAP  to print multiplication table of first 10 natural numbers.

Ans:

#include<stdio.h>

#include<conio.h>

main()

{

int i,j,c;

for(i=1;i<=10;++i)

{         

for(j=1;j<=10;++j)

{

c=i*j;

printf("%dX%d=%d\t",i,j,c);

}

printf("\n");

}

getch();

}

6. WAP  to display the factorial number of the first 10 natural numbers.

Ans:

#include<stdio.h>

#include<conio.h>

main()

{

int i,j,f;

for(i=1;i<=10;++i)

{

  f=1;

  for(j=i;j>0;--j)

  {

   f=f*j;

  }

  printf("%d=%d\n",i,f);

  }

  getch();

}

7. Write a program to prime numbers up to 500 numbers.

Ans:

#include<stdio.h>

#include<conio.h>

main()

{

int i,j;

for(i=2;i<=100;++i)

{

for(j=2;j<=i;++j)

{

   if(i%j==0)

   break;

}

 if(i==j)

 printf("%d\t",i);

}

 getch();

}

8. Write a program to display the following pattern

*

**

***

****

*****

Ans:

#include <stdio.h>

#include<conio.h>

main()

{

    int x=1,y;

    do

    {

        y=1;

        do

        {

            printf("*");

            y++;

        }while(y <= x);

        x++;

        printf("\n");

    }while(x <= 5);

    getch();

}

 

You may also like:

How to Installation Download QBasic 64 Bit: A Beginner's Guide

Sorting array elements by using Structure in C Programming

C program to read name and marks of 5 different subjects calculate total percentage and display them in tabular format using structure

QBASIC program to display the Perfect Square numbers from 1 to 50.

C program to sort names in alphabetical ascinding order using-structures

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages