أخر الاخبار

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

Q) Write a C# program to find the value of z using while 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 = 0; // Initialize z to avoid potential errors

        // Loop to handle invalid input (optional)
        while (i < 1 || i > 4)
        {
            Console.WriteLine("Invalid value for i. Please enter 1, 2, 3, or 4.");
            Console.Write("Enter the value of i again: ");
            i = int.Parse(Console.ReadLine());
        }

        // Calculate z based on the value of i
        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;
        }

        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 while..

Sol//

While loops are ideal for iterative processes, but they're not strictly necessary for a single calculation and comparison. Here's a flowchart that combines user input, area calculation, and the area check in one pass:

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

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;

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

        // Input validation loop (while)
        while (true)
        {
            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 && i <= 3) // Check for valid i (1, 2, or 3)
            {
                break; // Exit loop if valid input is received
            }
            Console.WriteLine("Invalid value for i. Please enter 1, 2, or 3.");
        }

        // Calculation based on i
        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;
        }

        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 while.

Sol//


+----------------+    +-------------------+
| Start           | -> | Set loop condition | (True)
+----------------+    +-------------------+
         v                   v
+----------------+           +----------------+
| Read number x   |           |  Is x divisible  |
+----------------+           |  by 4 (x % 4==0)? |
         v                   v
+----------------+           +----------------+
| Read number y   |           |        Yes        |
+----------------+           |                   v
         v                   +----------------+
+----------------+           | Swap values of  |
|  Set loop        |           | x and y         |
|  condition to   |           +----------------+
|  False          |                   v
         v                   +----------------+
+----------------+           | Print x and y   |
| While loop       |           +----------------+
|  condition is   |                   v
|   True           |           +----------------+
+----------------+           | End             |
         v                   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 while?

Sol//

using System;

public class SwapIfOddAndDivisibleByFive_WhileLoop
{
    public static void Main(string[] args)
    {
        bool swapped = false; // Flag to track if swap happened

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

        // Loop until swapped or user exits (can be improved)
        while (true)
        {
            // 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;

                swapped = true; // Set flag to indicate swap
                break; // Exit loop after swap
            }
            else
            {
                // Offer option to exit if not swapped
                Console.WriteLine("Numbers not swapped (conditions not met). Exit? (y/n)");
                string exit = Console.ReadLine().ToLower();
                if (exit == "y")
                {
                    break; // Exit loop if user chooses to exit
                }
            }
        }

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

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

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
        while (true)
        {
            if (average > 50)
            {
                Console.WriteLine("Pass");
            }
            else
            {
                Console.WriteLine("Failed");
            }
            break;
        }
    }
}


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 while?

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):");

        int num = Convert.ToInt32(Console.ReadLine());

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

            num = Convert.ToInt32(Console.ReadLine());
        }

        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 while

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

Sol//

using System;

public class CalculateZ
{
    public static void Main(string[] args)
    {
        // Read values for a, b, and n
        double a, b, n;
        Console.Write("Enter the value of a: ");
        a = double.Parse(Console.ReadLine());

        Console.Write("Enter the value of b (cannot be zero): ");
        b = double.Parse(Console.ReadLine());
        while (b == 0) // Input validation for non-zero b
        {
            Console.WriteLine("Error: b cannot be zero. Please enter a non-zero value for b: ");
            b = double.Parse(Console.ReadLine());
        }

        Console.Write("Enter the value of n (positive integer): ");
        n = double.Parse(Console.ReadLine());
        while (n <= 0) // Input validation for positive n
        {
            Console.WriteLine("Error: n must be a positive integer. Please enter a positive value for n: ");
            n = double.Parse(Console.ReadLine());
        }

        // Calculate the value of z
        double z = 0;
        for (int i = 2; i <= n + 1; i++)
        {
            double term = i * (a + b) / ((i + 1) * b);
            z += term;
        }

        // Print the result
        Console.WriteLine($"\nThe value of z is: {z:F2}");
    }
}



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

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;
        int i = 2;

        while (i <= n)
        {
            if (i == 2)
            {
                z += (2 * Math.Pow(x, 3)) / (2 + y);
            }
            else
            {
                z += (i * Math.Pow(x, i + 1)) / (i + y);
            }
            i++;
        }

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


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


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;
        int i = 1;

        while (i <= n)
        {
            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;
            }
            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 while?

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

        // Check if the first number is divisible by 3
        if (num % 3 == 0)
        {
            Console.WriteLine("No numbers entered.");
            return;
        }

        while (num % 3 != 0)
        {
            sum += num;
            count++;

            num = Convert.ToInt32(Console.ReadLine());
        }

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



تعليقات



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