Q) Write a C# program to find the value of z using do-while statement :
z = {
2xy + x if i= 1
3xy − x if i= 2
4xy/x if i= 3
5xy ∗ x if i= 4 }
sol//
Sol//
The do-while loop makes the program calculate the area and check the condition at least once. Here's the flowchart:
+-----------------+
| Start |
+-----------------+
v (do at least once)
+-----------------+
| 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 (check loop condition)
+-----------------+
| Exit (if i > 0) | | i-- |
+-----------------+-------v
+---------+
| i > 0? |
+---------+
v
(no) / (yes)
+---------+
| Exit |
+---------+
C# program
using System;
public class RectangleArea
{
public static void Main(string[] args)
{
int length, width, area;
int i = 1; // Loop counter (initialized to 1 for do-while execution)
do
{
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.");
}
i--; // Decrement loop counter
} while (i > 0); // Loop continues as long as i is positive
}
}
Sol//
using System;
public class FindZValue
{
public static void Main(string[] args)
{
int x, y, i;
double z; // Use double for floating-point calculations
do
{
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)
{
Console.WriteLine("Invalid value for i. Please enter 1, 2, or 3.");
}
else
{
break; // Exit loop if valid input for i is entered
}
} while (true); // Loop continues until valid input is received
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 do-while.
Sol//
+----------------+
| Start |
+----------------+
v
+----------------+
| Read number x |
+----------------+
v
+----------------+
| Read number y |
+----------------+
v
+----------------+ +----------------+
| Is x divisible | | Yes (Swap) |
| by 4 (x % 4==0)? | ----> | |
+----------------+ v
v +----------------+
+----------------+ | Swap values of |
| No | ----> | x and y |
+----------------+ v
v +----------------+
+----------------+ | Print x and y |
| Print x and y | ----> | (unchanged) |
+----------------+ v
v +----------------+
+----------------+ | End |
| | 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 do-while?
Sol//
Q/ Write C# program to find the value of z using the formula: using do-while
Sol//
Q/ Write C# program to find the value of z using the formula: using do-while
Sol//
Q) Write a C# program to find the value of Y? using do-while.
Sol//
Q) Write C# program to find the average of set numbers ended by number divided by 3. using do-while?
Sol//