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

Find the output of C#.Net programs | if else | Set 3: 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   A = 20;
            float B = 20.0F;

            if (sizeof(A) == sizeof(B))
                Console.WriteLine("Hello");
            else
                Console.WriteLine("Hiii");
        }
    }
}

Output:

main.cs(13,24): error CS0246: The type or namespace name `A' could not be found.
Are you missing an assembly reference?

Explanation:

The above program will generate syntax error because we cannot use variables with sizeof() operator in C#.

Question 2:

using System;

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

            if (A>B)
                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. Because A is the double type and B is float type. In C#, a double number is always treated bigger in case of equality. That's why the If condition will true and print "Hello" on the console screen.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string STR1 = "HELLO";
            string STR2 = "hello";

            if (STR1>STR2)
                Console.WriteLine("WWW.INCLUDEHELP.COM");
            else
                Console.WriteLine("WWW.GOOGLE.COM");
        }
    }
}

Output:

main.cs(13,17): error CS0019: Operator `>' cannot be applied to 
operands of type `string' and `string'

Explanation:

The above program will generate syntax error because we cannot use greater than '>' operator with strings.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string STR1 = "HELLO";
            string STR2 = "hello";

            if (STR1==STR2)
                Console.WriteLine("WWW.INCLUDEHELP.COM");
            else
                Console.WriteLine("WWW.GOOGLE.COM");
        }
    }
}

Output:

WWW.GOOGLE.COM
Press any key to continue . . .

Explanation:

The above program will print "WWW.GOOGLE.COM" on the console screen. Because STR1 and STR2 do not contain the same values. That's why condition will false and else part will execute.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string STR1 = "HELLO";
            string STR2 = "hello";

            if (STR1 == STR2.ToUpper())
                Console.WriteLine("WWW.INCLUDEHELP.COM");
            else
                Console.WriteLine("WWW.GOOGLE.COM");
        }
    }
}

Output:

WWW.INCLUDEHELP.COM
Press any key to continue . . .

Explanation:

The above program will print "WWW.INCLUDEHELP.COM" on the console screen. Here, we declared two string variables STR1 and STR2 initialized with "HELLO" and "hello" respectively.

Here, we used the ToUpper() method that will return "HELLO" then the condition will be true and print "WWW.INCLUDEHELP.COM" on the console screen.





Comments and Discussions!

Load comments ↻





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