ADVERTISEMENT
ADVERTISEMENT

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

Find the output of C#.Net programs | if else | Set 2: 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 SUB1 = 60;
            int SUB2 = 68;
            int SUB3 = 97; 
            int SUB4 = 48;
            int SUB5 = 57;

            float PER = 0.0;

            PER = (SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;

            if (PER > 60)
                Console.WriteLine("First Division");
            else if (PER > 45)
                Console.WriteLine("Second Division");
            else if (PER > 35)
                Console.WriteLine("Third Division");
            else
                Console.WriteLine("Fail");
        }
    }
}

Output:

main.cs(16,25): error CS0664: Literal of type double cannot be implicitly converted to type `float'. 
Add suffix `f' to create a literal of this type

Explanation:

In the above program, we initialized variable PER with 0.0, which is a double value, that's why syntax error will be generated. We need to use suffix 'F' to initialize a float number. The correct way is given below:

float PER = 0.0F;

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int SUB1 = 60;
            int SUB2 = 60;
            int SUB3 = 77; 
            int SUB4 = 48;
            int SUB5 = 47;

            float PER = 0.0F;

            PER = (SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;

            if (PER > 60)
                Console.WriteLine("First Division");
            if (PER > 45)
                Console.WriteLine("Second Division");
            if (PER > 35)
                Console.WriteLine("Third Division");
            else
                Console.WriteLine("Fail");
        }
    }
}

Output:

Second Division
Third Division
Press any key to continue . . .

Explanation:

In the above program, we created 5 integer variables for subjects and variable PER of float type to calculate the percentage.

Now look to the expression:

PER =(SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;
PER = (60+60+77+48+47)/5;
PER = (60+60+77+48+47)/5;
PER = (292)/5;
PER = 58;

Then conditions if (PER > 45) and if (PER > 45) will true then message "Second Division" and "Third Division" will print on the console screen.

Question 3:

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;

            if (B > A)
            {
                if (A > C)
                {
                    Console.WriteLine("ABC");
                }
                else
                {
                    if (C > B)
                    {
                        Console.WriteLine("GHI");
                    }
                    else
                    {
                        Console.WriteLine("LMN");
                    }
                }
            }
            else
            {
                Console.WriteLine("PQR");
            }
        }
    }
}

Output:

GHI

Explanation:

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

Let's look at the conditions:

The condition if (B > A) that is if(20>10) condition will true, and then condition if (A > C) that is if(10>30), will false and then else part will execute and condition if (C > B) will true and print "GHI" on the console screen.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 10;
            int B = 20;
 
            if (A > B ? true : false)
                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 integer variables A and B that are initialized with 10, 20 respectively.

if (A > B ? true : false)

In the above condition, we used a conditional operator inside the If condition. The conditional operator will return false. That's why the else part will execute and print "www.google.com" on the console screen.

Question 5:

using System;

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

            if (A > B && A == 10)
                Console.WriteLine("ABC");
            else if(C>B && C=20)
                Console.WriteLine("PQR");
            else if(C==30 && C==10)
                Console.WriteLine("XYZ");
        }
    }
}

Output:

main.cs(16,28): error CS1525: Unexpected symbol `='

Explanation:

The above program will generate a syntax error,

else if(C>B && C=20)

In the above condition, we used the assignment operator "=" instead of equal to "==" operator. Then it will return an integer value that cannot be used as an operand with the logical end "&&" operator.



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.