Q) Write a C# program to find the value of z using switch statement :
z = {
2xy + x if i= 1
3xy − x if i= 2
4xy/x if i= 3
5xy ∗ x if i= 4 }
Sol//
The flowchart for this program will look like this:
+-----------------+
| Start |
+-----------------+
v
+-----------------+
| Enter length |
+-----------------+
v
+-----------------+
| Enter width |
+-----------------+
v
+-----------------+
| Calculate area |
| (length * width) |
+-----------------+
v
+-----------------+
| Area > 50? |
+-----------------+
v
+---------+ +---------+
| Yes | ----> | No |
+---------+ +---------+
v v
+-----------------+ +-----------------+
| Print "Area > 50"| |Print "Area <= 50"|
+-----------------+ +-----------------+
v v
+-----------------+
| End |
+-----------------+
C# program
using System;
public class RectangleArea
{
public static void Main(string[] args)
{
int length, width, area;
Console.Write("Enter the length of the rectangle: ");
length = int.Parse(Console.ReadLine());
Console.Write("Enter the width of the rectangle: ");
width = int.Parse(Console.ReadLine());
area = length * width;
switch (area > 50) // Switch on a boolean expression
{
case true:
Console.WriteLine("The area of the rectangle is greater than 50.");
break;
case false:
Console.WriteLine("The area of the rectangle is less than or equal to 50.");
break;
}
}
}
using System;
public class FindZValue
{
public static void Main(string[] args)
{
int x, y, i;
Console.Write("Enter the value of x: ");
x = int.Parse(Console.ReadLine());
Console.Write("Enter the value of y: ");
y = int.Parse(Console.ReadLine());
Console.Write("Enter the value of i (1, 2, or 3): ");
i = int.Parse(Console.ReadLine());
double z; // Use double for floating-point calculations
switch (i)
{
case 1:
z = Math.Sqrt(Math.Pow(x, 2) * Math.Pow(y, 2) + x * y + 5);
break;
case 2:
z = Math.Abs(3 * Math.Pow(x, 2) - 2 * Math.Pow(y, 2) - 5);
break;
case 3:
if (x == 0)
{
Console.WriteLine("Error: Division by zero is not allowed.");
return;
}
z = 4.0 * x * y / x; // Cast x to double for division accuracy
break;
default:
Console.WriteLine("Invalid value for i. Please enter 1, 2, or 3.");
return;
}
Console.WriteLine("The value of z is: " + z);
}
}
Q) Draw a flowchart to read two numbers x and y , and swap the two numbers if x divided by 4 using Switch.
Sol//
+----------------+
| Start |
+----------------+
v
+----------------+ +----------------+
| Read number x | | x % 4 == 0 ? |
+----------------+ +----------------+
v v
+----------------+ |
| Read number y | Yes (Swap) |
+----------------+ v
v +----------------+
+----------------+ | Swap values of |
| x | ----> | x and y |
+----------------+ v
v +----------------+
+----------------+ | Print x and y |
| y | ----> | (unchanged) |
+----------------+ v
v +----------------+
+----------------+ | End |
| Print x and y | v
+----------------+ +----------------+
Q/ Write a C# program to read two integer numbers (a, b) and swap the two numbers if a and b are odd and a is divided by 5.using Switch?
Sol//
Q/ Write C# program to find the value of z using the formula: using Switch
Sol//
Q/ Write C# program to find the value of z using the formula: using Switch
Sol//
Q) Write a C# program to find the value of Y? using Switch.
Sol//
Q) Write C# program to find the average of set numbers ended by number divided by 3. using Switch?
Sol//