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.
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");
}
}
}
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");
}
}
}
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");
}
}
}
}
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");
}
}
}
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");
}
}
}
Answer 1 :
Output:
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;
Answer 2:
Output:
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.
Answer 3:
Output:
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.
Answer 4:
Output:
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.
Answer 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer