أخر الاخبار

تحديات Switch في لغة C#: حلول مبتكرة لأسئلة متنوعة

 

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//

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, 3, or 4): ");
        i = int.Parse(Console.ReadLine());

        int z;
        switch (i)
        {
            case 1:
                z = 2 * x * y + x;
                break;
            case 2:
                z = 3 * x * y - x;
                break;
            case 3:
                if (x == 0) // Handle division by zero
                {
                    Console.WriteLine("Error: Division by zero is not allowed.");
                    return;
                }
                z = 4 * x * y / x;
                break;
            case 4:
                z = 5 * x * y * x;
                break;
            default:
                Console.WriteLine("Invalid value for i. Please enter 1, 2, 3, or 4.");
                return;
        }

        Console.WriteLine("The value of z is: " + z);
        Console.ReadKey();
    }
}



Q) Draw a flowchart and write C# program to calculate area for rectangle and test if the area is greater than
50 or not? using switch..

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

C#
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;
        }
    }
}


Q) Write C# program to find the value of z using the formula: using Switch

Write C# program to find the value of z using the formula:


Sol//


C#
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//

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter two integer numbers (a and b):");

        // Reading two integer numbers a and b from the user
        int a = Convert.ToInt32(Console.ReadLine());
        int b = Convert.ToInt32(Console.ReadLine());

        // Using a switch statement to check conditions and swap if necessary
        switch (a % 5 == 0 && a % 2 != 0 && b % 2 != 0)
        {
            case true:
                // Swap the values of a and b
                int temp = a;
                a = b;
                b = temp;
                Console.WriteLine("Numbers swapped successfully!");
                break;
            case false:
                Console.WriteLine("Numbers not swapped.");
                break;
        }

        // Displaying the values of a and b after potential swap
        Console.WriteLine($"Value of a: {a}");
        Console.WriteLine($"Value of b: {b}");
    }
}


Q / Write a C# program to read the student’s name, gender, three scores and find the average of the scores then print “pass” when the average is greater than 50, otherwise print “failed”. using switch?

Sol//
using System;

public class StudentResult
{
    public static void Main(string[] args)
    {
        // Read student information
        Console.Write("Enter student name: ");
        string name = Console.ReadLine();

        Console.Write("Enter student gender (M/F): ");
        char gender = char.ToUpper(Console.ReadKey().KeyChar); // Read and convert to uppercase

        Console.WriteLine(); // New line for better formatting

        // Read three scores
        double score1, score2, score3;
        Console.Write("Enter score 1: ");
        score1 = double.Parse(Console.ReadLine());

        Console.Write("Enter score 2: ");
        score2 = double.Parse(Console.ReadLine());

        Console.Write("Enter score 3: ");
        score3 = double.Parse(Console.ReadLine());

        // Calculate average score
        double average = (score1 + score2 + score3) / 3;

        // Determine result (pass/fail)
        string result;
        if (average > 50)
        {
            result = "Pass";
        }
        else
        {
            result = "Fail";
        }

        // Print student information and result
        Console.WriteLine($"\nStudent Name: {name}");

        // Optional: Use switch for gender (can be replaced with an if-else if preferred)
        switch (gender)
        {
            case 'M':
                Console.WriteLine("Gender: Male");
                break;
            case 'F':
                Console.WriteLine("Gender: Female");
                break;
            default:
                Console.WriteLine("Gender: Unknown");
                break;
        }

        Console.WriteLine($"Average Score: {average:F2}"); // Format average to two decimal places
        Console.WriteLine($"Result: {result}");
    }
}



Q/ Write C# program to find the count of all numbers less than 50 and find the summation of all numbers greater than or equal 50 , in a set of numbers ended by zero number. using switch?

Sol//
using System;

class Program
{
    static void Main(string[] args)
    {
        int countLessThan50 = 0;
        int sumGreaterThanOrEqualTo50 = 0;

        Console.WriteLine("Enter a set of numbers (enter 0 to end):");

        while (true)
        {
            int num = Convert.ToInt32(Console.ReadLine());

            if (num == 0)
                break;

            switch (num < 50)
            {
                case true:
                    countLessThan50++;
                    break;
                case false:
                    sumGreaterThanOrEqualTo50 += num;
                    break;
            }
        }

        Console.WriteLine($"Count of numbers less than 50: {countLessThan50}");
        Console.WriteLine($"Summation of numbers greater than or equal to 50: {sumGreaterThanOrEqualTo50}");
    }
}


Q/ Write C# program to find the value of z using the formula: using Switch

Q3/ Write C# program to find the value of z using the formula:

Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the values of a, b, and n:");

        // Reading values of a, b, and n from the user
        double a = Convert.ToDouble(Console.ReadLine());
        double b = Convert.ToDouble(Console.ReadLine());
        int n = Convert.ToInt32(Console.ReadLine());

        double sum = 0;

        // Computing the sum using a switch statement
        for (int i = 2; i <= n; i++)
        {
            switch (i)
            {
                case 2:
                    sum += 2 * (a + b) / (3 * b);
                    break;
                default:
                    sum += i * (a + b) / ((i + 1) * b);
                    break;
            }
        }

        Console.WriteLine($"The value of Z is: {sum}");
    }
}


Q/ Write C# program to find the value of z using the formula: using Switch

Q2/ Write C# program to find the value of z using the formula:

Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the value of x:");
        double x = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Enter the value of y:");
        double y = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Enter the value of n:");
        int n = Convert.ToInt32(Console.ReadLine());

        double z = 0;

        for (int i = 2; i <= n; i++)
        {
            switch (i)
            {
                case 2:
                    z += (2 * Math.Pow(x, 3)) / (2 + y);
                    break;
                default:
                    z += (i * Math.Pow(x, i + 1)) / (i + y);
                    break;
            }
        }

        Console.WriteLine($"The value of z is: {z}");
    }
}


Q) Write a C# program to find the value of Y? using Switch.


Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the value of X:");
        double x = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Enter the value of n:");
        int n = Convert.ToInt32(Console.ReadLine());

        double y = 5;

        for (int i = 1; i <= n; i++)
        {
            switch (i)
            {
                case 1:
                    y += (2 * Math.Pow(x, i)) / i;
                    break;
                case 2:
                    y += (4 * Math.Pow(x, i)) / i;
                    break;
                default:
                    y += ((i + 1) * Math.Pow(x, i)) / i;
                    break;
            }
        }

        Console.WriteLine($"The value of Y is: {y}");
    }
}


Q) Write C# program to find the average of set numbers ended by number divided by 3. using Switch?

Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        int sum = 0;
        int count = 0;

        Console.WriteLine("Enter numbers (end with a number divisible by 3):");

        int num;
        bool endLoop = false;

        do
        {
            num = Convert.ToInt32(Console.ReadLine());

            switch (num % 3)
            {
                case 0:
                    if (count == 0)
                    {
                        Console.WriteLine("No numbers entered.");
                    }
                    else
                    {
                        double average = (double)sum / count;
                        Console.WriteLine($"Average of the numbers: {average}");
                    }
                    endLoop = true;
                    break;
                default:
                    sum += num;
                    count++;
                    break;
            }

        } while (!endLoop);
    }
}




تعليقات



حجم الخط
+
16
-
تباعد السطور
+
2
-