ADVERTISEMENT
ADVERTISEMENT

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

Find the output of C#.Net programs | Loops | Set 2: 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)
        {
            for (; ; )
            {
                Console.WriteLine("Hello");
            }
        }
    }
}

Output:

"Hello" will be printed infinite times on the console screen.

Explanation:

In the above program, we did not use loop condition, if we do not use loop condition in the for loop, then the condition will be considered as true in C#, that's why it will print "Hello" infinite times on the console screen.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            while()
            {
                Console.WriteLine("Hello");
            }
        }
    }
} 

Output:

main.cs(10,18): error CS1525: Unexpected symbol `)'

Explanation:

In the above program, we did not use "loop condition" in the "while" loop, that's why the above program will generate a syntax error.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num=5;
            int i=1;
            do
            {
                Console.WriteLine(num*i);
            }while(i<=10)
        }
    }
}

Output:

main.cs(16,8): error CS1525: Unexpected symbol `}', expecting `;'

Explanation:

The above program will generate syntax error because missed semicolon after loop condition, the correct code is given below:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num=5;
            int i=1;
            do
            {
                Console.WriteLine(num*i);
            }while(i<=10);
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num=5439;
            var var = 0;
            while (num>0)
            {
                num = num / 10;
                var++;
            }

            Console.WriteLine(var);
        }
    }
} 

Output:

4
Press any key to continue . . .

Explanation:

In the above program, we created integer num initialized with 5439, and we also created an implicitly typed variable var which is initialized with 0. Here, loop will execute until the value of variable num is greater than 0.

In the body of the loop, we divide the variable num by 10 and assigned it to the same.

Now look iterations:

Iteration1:

num=5439 and var=0

num = 5439/10;

num = 543;

And we increased the value of "var" then it becomes 1.

Iteration3:

num=54 and var=2

num = 54/10;

num = 5;

And we increased the value of "var" then it becomes 3.

Iteration4:

num=5 and var=3

num = 5/10;

num = 0;

And we increased the value of "var" then it becomes 4. After that loop will terminate because the condition will false.

Question 5:

using System;

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

            while (num>0)
            {
                ok = num % 10;
                num = num / 10;

                res += (ok*ok*ok);
            }
            Console.WriteLine(res);
        }
    }
}   

Output:

153
Press any key to continue . . .

Explanation:

In the above program, we created three integer variables num, ok, res initialized with 153, 0, and 0.

Let's understand the while loop using iterations:

Iteration1:
num=153, ok=0, res=0
Loop condition is true.
ok = 153%10 	   = 3;
num = 153/10 	   = 15;
res = 0 + (3*3*3)  =  27;

Iteration2:
num=15, ok=3, res=27
Loop condition is true.
ok = 15%10 	   = 5;
num = 15/10 	   = 1;
res = 27 + (5*5*5)  =  27+125= 152;

Iteration3:
num=1, ok=5, res=152
Loop condition is true.
ok = 1%10 	   = 1;
num = 1/10 	   = 0;
res = 152 + (1*1*1)  =  152+1= 153;

Now the condition will true and the loop will terminate and then print 153 on the console screen.



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.