0% found this document useful (0 votes)
7 views7 pages

C# Program for Score and Discount Calculation

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

C# Program for Score and Discount Calculation

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 2

using System
class Program
{
static void Main(string[] args)
{
[Link]("Enter first test score: ");
double score1 = GetValidScore();
[Link]("Enter second test score: ");
double score2 = GetValidScore();
[Link]("Enter third test score: ");
double score3 = GetValidScore();
double highest = [Link]([Link](score1, score2), score3);
double lowest = [Link]([Link](score1, score2), score3);
[Link]($"Highest score: {highest}");
[Link]($"Lowest score: {lowest}");
if (score1 > 75 && score2 > 75 && score3 > 75)
{
[Link]("Distinction");
}
if (score1 < 50 || score2 < 50 || score3 < 50)
{
[Link]("Needs Improvement");
}
double average = (score1 + score2 + score3) / 3;
[Link]($"Average score: {average}");
if (average >= 90)
{
[Link]("Grade: A");
}
else if (average >= 80)
{
[Link]("Grade: B");
}
else if (average >= 70)
{
[Link]("Grade: C");
}
else if (average >= 60)
{
[Link]("Grade: D");
}
else
{
[Link]("Grade: F");
}
}
static double GetValidScore()
{
while (true)
{
if ([Link]([Link](), out double score))
{
if (score >= 0)
{
return score;
}
else
{
[Link]("Score cannot be negative. Please try again.");
}
}
else
{
[Link]("Invalid input. Please enter a valid score.");
}
}
}
}
Question 3

using System;
class Program
{
static void Main(string[] args)
{
int n = GetValidNumber();
[Link]("Multiplication Table:");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
[Link]($"{i} x {j} = {i * j}\t");
}
[Link]();
}
[Link]("Do you want to save the table to a file? (yes/no): ");
string response = [Link]();
if ([Link]() == "yes")
{
SaveTableToFile(n);
}
}
static int GetValidNumber()
{
while (true)
{
[Link]("Enter a positive integer (1-15): ");
if ([Link]([Link](), out int n))
{
if (n >= 1 && n <= 15)
{
return n;
}
else if (n < 1)
{
[Link]("Number must be positive. Please try again.");
}
else
{
[Link]("Number must be between 1 and 15. Please try again.");
}
}
else
{
[Link]("Invalid input. Please enter a valid number.");
}
}
}
static void SaveTableToFile(int n)
{
string filePath =
$"multiplication_table_{[Link]("yyyyMMddHHmmss")}.txt";
using (StreamWriter writer = new StreamWriter(filePath))
{
[Link]("Multiplication Table:");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
[Link]($"{i} x {j} = {i * j}\t");
}
[Link]();
}
}
[Link]($"Table saved to {filePath}");
}
}
Question 4
class Program
{
static void Main(string[] args)
{
[Link]("Enter the number of items: ");
int numItems = GetValidNumber();

string[] itemNames = new string[numItems];


double[] itemPrices = new double[numItems];

double totalCost = 0;

for (int i = 0; i < numItems; i++)


{
[Link]($"Enter item {i + 1} name: ");
itemNames[i] = [Link]();

[Link]($"Enter item {i + 1} price: ");


itemPrices[i] = GetValidPrice();

totalCost += itemPrices[i];
}
[Link]("Membership Types:");
[Link]("1. Silver");
[Link]("2. Gold");
[Link]("3. Platinum");
[Link]("Enter your membership type (1/2/3): ");
int membershipType = GetValidMembershipType();
double discount = 0;
if (membershipType == 1 && totalCost > 100)
{
discount = totalCost * 0.05;
}
else if (membershipType == 2 && totalCost > 200)
{
discount = totalCost * 0.10;
}
else if (membershipType == 3 && totalCost > 300)
{
discount = totalCost * 0.15;
}
if (totalCost > 500)
{
discount += totalCost * 0.10;
}
double finalAmount = totalCost - discount;
[Link]($"Total Cost: ${totalCost:F2}");
[Link]($"Discount Applied: ${discount:F2}");
[Link]($"Final Amount: ${finalAmount:F2}");
}
static int GetValidNumber()
{
while (true)
{
if ([Link]([Link](), out int num))
{
if (num > 0)
{
return num;
}
else
{
[Link]("Number must be positive. Please try again.");
}
}
else
{
[Link]("Invalid input. Please enter a valid number.");
}
}
}

static double GetValidPrice()


{
while (true)
{
if ([Link]([Link](), out double price))
{
if (price > 0)
{
return price;
}
else
{
[Link]("Price must be positive. Please try again.");
}
}
else
{
[Link]("Invalid input. Please enter a valid price.");
}
}
}
static int GetValidMembershipType()
{
while (true)
{
if ([Link]([Link](), out int membershipType))
{
if (membershipType >= 1 && membershipType <= 3)
{
return membershipType;
}
else
{
[Link]("Invalid membership type. Please enter 1, 2, or 3.");
}
}
else
{
[Link]("Invalid input. Please enter a valid membership type.");
}
}
}
}

Common questions

Powered by AI

The program checks if all scores are above 75 to grant a 'Distinction'. Conversely, if any score is below 50, it indicates 'Needs Improvement' .

Membership type dictates the discount rate, with Silver members eligible for a 5% discount on orders over $100, Gold 10% on orders over $200, and Platinum 15% on orders over $300. Additionally, a 10% discount applies for orders over $500, combining with the membership discount, significantly affecting total savings a user can achieve .

The program prompts for a number between 1 and 15, repeatedly until a valid number is entered. This ensures the table fits the expected size range, preventing unrealistic or cumbersome outputs and potential performance issues with large tables .

Conditional statements control the execution path by evaluating test scores and determining distinctions, performance improvement needs, and grade assignments. They ensure that only appropriate responses are triggered based on precise input conditions, emphasizing the correct logical flow and preventing incorrect output. This significantly impacts program outcomes by allowing for responsiveness and adaptability based on user-provided data .

An item's price is accepted if it is a positive double value. The program uses a loop to request input until a valid price is entered, providing feedback if the input is invalid or non-positive, to ensure only legitimate prices are used in calculations .

The program uses a loop to repeatedly prompt the user for input until a valid score is entered. A score is considered valid if it is a non-negative double value. This is important to ensure that only legitimate scores are processed, preventing errors in calculations or outputs .

The program employs input validation within a loop to check each entered score. If a score is negative, it prompts the user with a message specifying that scores cannot be negative and requests re-entry until a valid score is provided. This ensures data integrity by preventing negative values from affecting the program's logical operations .

The file saving functionality allows users to preserve the multiplication table for future reference, which can be useful for educational purposes or documentation. The program uses a StreamWriter to save the table as a text file named with a timestamp, ensuring each file is uniquely identified .

The program calculates a student's average test score by summing three individual scores and dividing by three. It then assigns a grade based on the following conditions: if the average is 90 or above, the grade is 'A'; if 80 or above, 'B'; if 70 or above, 'C'; if 60 or above, 'D'; otherwise, the grade is 'F' .

Different discount rates incentivize different levels of spending based on membership benefits. The program assigns a 5% discount for Silver members with a total cost over $100, 10% for Gold with over $200, and 15% for Platinum with over $300. Additionally, if total cost exceeds $500, an extra 10% discount applies. This is implemented using conditional statements that check membership type and total cost .

You might also like