ADVERTISEMENT
ADVERTISEMENT

C#.Net find output programs (Loops) | set 3

Find the output of C#.Net programs | Loops | Set 3: Enhance the knowledge of C#.Net Loops concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 06, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num =   5;

            for (int i = 0, j = 0; i < num; i++, j++)
            {
                Console.WriteLine(i * j);
            }
        }
    }
}

Output:

0
1
4
9
16
Press any key to continue . . .

Explanation:

In the above program, we created a variable num with initial value 5.

Now look to the loop iterations:

Iteration1:
i=0 , j =0, here loop condition is true
It will print 0 
Increase the value of 'i' and 'j'

Iteration2:
i=1 , j =1, here loop condition is true
It will print 1
Increase the value of 'i' and 'j'

Iteration3:
i=2 , j =2, here loop condition is true
It will print 4
Increase the value of 'i' and 'j'

Iteration4:
i=3 , j =3, here loop condition is true
It will print 9
Increase the value of 'i' and 'j'

Iteration5:
i=4 , j =4, here loop condition is true
It will print 15
Increase the value of 'i' and 'j'

Now condition will false, because the value i is 5, which is not less than num, and the loop will terminate.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num =   5;

            for (int i = 0;int j = 0; i < num; i++, j++)
            {
                Console.WriteLine(i * j);
            }
        }
    }
}

Output:

main.cs(12,31): error CS1525: Unexpected symbol `j', expecting `.'
main.cs(12,50): error CS1525: Unexpected symbol `,', expecting `;' or `}'
main.cs(12,55): error CS1525: Unexpected symbol `)', expecting `;' or `}'

Explanation:

The above program will generate multiple errors because we did not use correct syntax in the for loop, which is given below,

for (int i = 0;int j = 0; i < num; i++, j++)

The correct syntax is given below:

for (int i = 0,j = 0; i < num; i++, j++)

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num =   5;

            for (int i = 1; i <= num; i++)
            {
                for (int j = 1; j <= i; j++)
                    Console.Write(j + " ");
                Console.WriteLine();
            }
        }
    }
}

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Press any key to continue . . .

Explanation:

In the above program, we created a variable num initialized with 5. Here, we used a nested loop, where the outer loop is used for the number of rows printed, And the inner loop will print execute according to the value of i.

For i=1;
It will print 1

For i=2;
It will print 1 2

For i=3;
It will print 1 2 3

For i=4;
It will print 1 2 3 4

For i=5;
It will print 1 2 3 4 5

Then the loop will terminate.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num =   5;

            for (int I= 1; I <= num; I--)
            {
                if (I == (int)Math.PI)
                    continue;
                Console.WriteLine(I);

            }
        }
    }
}

Output:

Infinite loop

Explanation:

The program will execute infinite time because we used a post decrement operator instead of an increment operator that's why condition will always true and the loop will never terminate.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num =   5;

            for (int I= 1; I <= num; I++)
            {
                if (I == (int)Math.PI)
                    continue;
                Console.WriteLine(I);

            }
        }
    }
}

Output:

1
2
4
5
Press any key to continue . . .

Explanation:

In the above program, we created a variable num initialized with 5. Here, loop will execute 5 times. In the loop body, we used PI property of Math class and typecast it into integer then it will be 3. When value of variable I will 3 then continue statement skip below WriteLine() statement. That's why it will not print '3' on the console screen.

Note: The continue is a skipping statement. It is used to skip, one or more than one statement based on the specified case.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.