أخر الاخبار

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

Q) Write a C# program to find the value of z using if-else 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;

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

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




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 if-else..

Sol//


The flowchart for calculating the area of a rectangle and checking if it's greater than 50 using an if-else statement looks 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
          +-----------------+
          |       End       |
          +-----------------+

C# program

Here's the C# program that implements the flowchart:

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());

        // Calculate area
        area = length * width;

        // Check area and print message
        if (area > 50)
        {
            Console.WriteLine("The area of the rectangle is greater than 50.");
        }
        else
        {
            Console.WriteLine("The area of the rectangle is less than or equal to 50.");
        }
    }
}



Q) Write C# program to find the value of z using the formula: using if-else
Q) Write C# program to find the value of z using the formula: using do-while

Sol//


C#
using System;

public class FindZValue
{
    public static void Main(string[] args)
    {
        int x, y, i;
        double z; // Use double for floating-point calculations

        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());

        if (i == 1)
        {
            z = Math.Sqrt(Math.Pow(x, 2) * Math.Pow(y, 2) + x * y + 5);
        }
        else if (i == 2)
        {
            z = Math.Abs(3 * Math.Pow(x, 2) - 2 * Math.Pow(y, 2) - 5);
        }
        else if (i == 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
        }
        else
        {
            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 if-else.

Sol//


+----------------+
| Start           |
+----------------+
         v
+----------------+
| Read number x   |
+----------------+
         v
+----------------+
| Read number y   |
+----------------+
         v
+----------------+  +-----------------------+
| Is x divisible  |  | Yes (Swap)             |
| by 4 (x % 4==0)? | ----> | Swap temporary = x;  |
+----------------+         |  x = y;                |
         v                 |  y = temporary;       |
+----------------+         +-----------------------+
         v                 v
+----------------+         +----------------+
|        No       | ----> | Print x and y   |
+----------------+         | (unchanged)      |
         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 if-else?

Sol//

using System;

public class SwapIfOddAndDivisibleByFive
{
    public static void Main(string[] args)
    {
        // Read two integer numbers from the user
        Console.Write("Enter the first number (a): ");
        int a = int.Parse(Console.ReadLine());

        Console.Write("Enter the second number (b): ");
        int b = int.Parse(Console.ReadLine());

        // Check if both a and b are odd and a is divisible by 5
        if (a % 2 != 0 && b % 2 != 0 && a % 5 == 0)
        {
            // Swap the values of a and b
            int temp = a;
            a = b;
            b = temp;

            Console.WriteLine("Numbers swapped (both odd and first is divisible by 5):");
        }
        else
        {
            Console.WriteLine("Numbers not swapped (conditions not met):");
        }

        // Print the swapped or original values
        Console.WriteLine($"a = {a}");
        Console.WriteLine($"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 if-else?

Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter student's name:");
        string name = Console.ReadLine();

        Console.WriteLine("Enter student's gender (M/F):");
        char gender = Convert.ToChar(Console.ReadLine());

        Console.WriteLine("Enter three scores:");
        double score1 = Convert.ToDouble(Console.ReadLine());
        double score2 = Convert.ToDouble(Console.ReadLine());
        double score3 = Convert.ToDouble(Console.ReadLine());

        // Calculating the average of the scores
        double average = (score1 + score2 + score3) / 3;

        // Printing whether the student passed or failed based on the average score
        if (average > 50)
        {
            Console.WriteLine("Pass");
        }
        else
        {
            Console.WriteLine("Failed");
        }
    }
}



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 if-else?

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;

            if (num < 50)
            {
                countLessThan50++;
            }
            else
            {
                sumGreaterThanOrEqualTo50 += num;
            }
        }

        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 if-else

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 if-else statements
        for (int i = 2; i <= n; i++)
        {
            if (i == 2)
            {
                sum += 2 * (a + b) / (3 * b);
            }
            else
            {
                sum += i * (a + b) / ((i + 1) * b);
            }
        }

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




Q/ Write C# program to find the value of z using the formula: using if-else

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++)
        {
            if (i == 2)
            {
                z += (2 * Math.Pow(x, 3)) / (2 + y);
            }
            else
            {
                z += (i * Math.Pow(x, i + 1)) / (i + y);
            }
        }

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



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


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++)
        {
            if (i == 1)
            {
                y += (2 * Math.Pow(x, i)) / i;
            }
            else if (i == 2)
            {
                y += (4 * Math.Pow(x, i)) / i;
            }
            else
            {
                y += ((i + 1) * Math.Pow(x, i)) / i;
            }
        }

        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 if-else?

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;
        do
        {
            num = Convert.ToInt32(Console.ReadLine());

            // Check if the number is divisible by 3
            if (num % 3 == 0)
            {
                break; // Exit the loop if divisible by 3
            }

            sum += num;
            count++;
        } while (true);

        // Calculate and display the average
        if (count > 0)
        {
            double average = (double)sum / count;
            Console.WriteLine($"Average of the numbers: {average}");
        }
        else
        {
            Console.WriteLine("No numbers entered.");
        }
    }
}



تعليقات



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