C#.Net find output programs (if else) | set 1

Find the output of C#.Net programs | if else | Set 1: Enhance the knowledge of C#.Net if else concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 05, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int VAL =(int)Math.PI;
            string str = "123";

            if (VAL == str.Length)
            {
                Console.WriteLine("Hello");
            }
            else
            {
                Console.WriteLine("Hiii");
            }
        }
    }
}

Output:

Hello
Press any key to continue . . .

Explanation:

The above program will print "Hello" on the console screen. In the above program, we created a variable VAL. Here, we used PI property of Math class and typecast into an integer, then VAL is initialized with 3.

We also created string str, which is initialized with "123". Now look to the below condition:

if (VAL == str.Length)

Here, str.Length will return 3, and the value of VAL is also 3, then condition gets true, and "Hello" will be printed on the console screen.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 10;
            int B = 20;
            int C = 30;

            C = ++A * --B + C;

            if (A==(++B*2))
            {
                Console.WriteLine("Hello: "+C);
            }
            else
            {
                Console.WriteLine("Hiii: "+C);
            }
        }
    }
}

Output:

Hiii: 239
Press any key to continue . . .

Explanation:

In the above program, we declared three integer variables A, B, and C initialized with 10, 20, 30.

Now evaluate the expression:

C = ++A * --B + C;

First, evaluate ++A, then A becomes 11, and then evaluate --B, then B becomes 19.

C = A *B + C; 
C = 11*19+30;
C = 209+30
C = 239

Let's check the condition given below:

if (A==(++B*2))
if (A==(20*2))
if (A==400)

A contains 11 then the If condition will false that's why "Hiii: 239" will be printed on the console screen.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float  A = 2.34F;
            double B = 2.34;

            if (A == B)
            {
                Console.WriteLine("www.includehelp.com");
            }
            else
            {
                Console.WriteLine("www.google.com");
            }
        }
    }
}

Output:

www.google.com
Press any key to continue . . .

Explanation:

In the above program, we declared two variables A and B that are initialized with "2.34F" and "2.34" respectively.

Here, A is float type, and B is double type then both numbers will not equal then condition get false and "www.google.com" will print on the console screen.

Question 4:

using System;

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

            if (A > null)
            {
                Console.WriteLine("www.includehelp.com");
            }
            else
            {
                Console.WriteLine("www.google.com");
            }
        }
    }
}

Output:

Warning: Comparing with null of type 'int?' always produces 'false'

www.google.com
Press any key to continue . . .

Explanation:

The above program will generate a warning and print "www.google.com". Here we compare an integer number with null then the result will always false. That's why "www.google.com" will print on the console screen.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int NUM = 0;
            
            string STR = "Hello World";
            
            NUM = STR.Length;

            if (NUM%2==0)
            {
                Console.WriteLine("NUM is EVEN number");
            }
            else
            {
                Console.WriteLine("NUM is ODD number");
            }
        }
    }
}

Output:

NUM is ODD number
Press any key to continue . . .

Explanation:

In the above program, we declared two variables NUM and STR that are initialized with 0 and "Hello World" respectively.

NUM = STR.Length;

Here, STR.Length will return 11 and assigned to the variable NUM.

if (NUM%2==0)
if (11%2==0)

In the above condition, we will get remainder 1, Then the condition gets false and print "NUM is ODD number" on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.