C# Console - Nested For Loop Examples

Console - Using Nested For Loop

The first thing to note when writing nested loops is that the parentheses of the loops should not be mixed. 

In each round of the outer loop, the inner loop will loop from start to finish.

For example, if the inner loop  is set to loop 10 times and the outer loop is set to loop 5 times, when the program runs, the inner loop will have run 50 times in total.

Nested For Loop Examples

Although the following examples may seem redundant, they will be very helpful in understanding the logic of using nested loops. You can easily reproduce these examples on the Console screen. Set up the loops that will print text on the screen as seen in the pictures.

The answers to some of the questions are written next to it.

Also, check the examples on the right.

Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= 10; j++)
    {
        Console.Write("* ");
    }
 
    Console.WriteLine();
}
 
Console.ReadKey();
 
Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= i; j++)
    {
        Console.Write("* ");
    }
 
    Console.WriteLine();
}
 
Console.ReadKey();
 
Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= i; j++)
    {
        console.Write(j.ToString() + " ");
    }
 
    Console.WriteLine();
}
 
Console.ReadKey();
 
Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= 10; j++)
    {
        Console.Write(i.ToString() + " ");
    }
 
    Console.WriteLine();
}
 
Console.ReadKey();
 
Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    console.Write(i.ToString() + " ");
 
    for (int j = 1; j <= i; j++)
    {
        Console.Write(i.ToString());
    }
 
    Console.WriteLine();
}
 
Console.ReadKey();
 
Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    Console.Write(i.ToString() + " ");
 
    for (int j = 1; j <= i; j++)
    {
        Console.Write(j.ToString());
    }
 
    Console.WriteLine();
}
 
Console.ReadKey();
 
Examples of nested for loops
for (int i = 1; i <= 10; i++)
{
    Console.Write("*");
 
    for (int j = 1; j <= i; j++)
    {
        Console.Write(" ");
    }
 
    Console.WriteLine("*");
}
 
Console.ReadKey();
 
Examples of nested for loops Examples of nested for loops
Examples of nested for loops Examples of nested for loops
Examples of nested for loops Examples of nested for loops

 

EXERCISES

Finding prime numbers in a range with a nested for loop

The program that writes the prime numbers between the two numbers entered by the user into label1 one under the other:

Console.Write("1. Number: ");
int a= int.Parse(Console.ReadLine());
Console.Write("2. Number: ");
int b= int.Parse(Console.ReadLine());
 
int x;
 
for (int i = a; i <= b; i++)
{
    x= 0;
 
    for (int j= 2; j< i; j++)
    {
        if (i % j == 0) x++;
    }
 
    if (x== 0) Console.WriteLine(i.ToString());
 
}
 
Console.ReadKey();

 

Finding prime numbers up to entered number

Numbers that are only divisible by 1 and itself are called prime numbers. Write the code of the program that finds the prime numbers starting from 1 up to the number to be entered from the keyboard.



COMMENTS




Read 952 times.

Online Users: 503



nested-for-loop-tutorials-console