using System;
namespace MO2033_Assignment1
{
class Program
{
static void Main(string[] args)
{
string[] fruits = {"apple", "banana", "cherry", "date", "elderberry"};
Console.WriteLine("List of fruits:");
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine($"{i + 1}. {fruits[i]}");
}
// Let's do something interesting: ask the user for their favorite fruit and inform if it's on the list
Console.WriteLine("\nWhich of the above fruits is your favorite?");
string userFruit = Console.ReadLine().ToLower();
bool isFruitFound = false;
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i] == userFruit)
{
isFruitFound = true;
break;
}
}
if (isFruitFound)
{
Console.WriteLine($"Great choice! {userFruit} is on the list.");
}
else
{
Console.WriteLine($"Sorry, {userFruit} is not on our list. You have a unique taste!");
}
}
}
}
using System;
namespace MO2033_Assignment1
{
class Program
{
static void Main(string[] args)
{
string constructedWord = "";
Console.WriteLine("Let's build a word! Enter characters one by one.");
Console.WriteLine("Enter '!' to stop and see the constructed word.");
while (true)
{
Console.WriteLine("Enter a character:");
char userInput = Console.ReadKey().KeyChar; // Read a single character from user input
if (userInput == '!')
{
Console.WriteLine("\nStopping word construction.");
break;
}
constructedWord += userInput; // Add the character to our word
Console.WriteLine($"\nCurrent word: {constructedWord}");
}
if (constructedWord.Length > 0)
{
Console.WriteLine($"Your constructed word is: {constructedWord}");
}
else
{
Console.WriteLine("You didn't add any characters to the word.");
}
}
}
}
csharpCopy code
using System;
namespace MO2033_Assignment1
{
class Calculator
{
static void Main(string[] args)
{
GreetUser("John"); // Test the greeting method with different names.
GreetUser("Alice");
Console.WriteLine("Welcome to the Calculator!");
while (true)
{
Console.WriteLine("\nSelect operation:\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit");
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 5)
{
Console.WriteLine("Thank you for using the Calculator. Goodbye!");
break;
}
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine($"Result: {Add(num1, num2)}");
break;
case 2:
Console.WriteLine($"Result: {Subtract(num1, num2)}");
break;
case 3:
Console.WriteLine($"Result: {Multiply(num1, num2)}");
break;
case 4:
try
{
Console.WriteLine($"Result: {Divide(num1, num2)}");
}
catch (DivideByZeroException)
{
Console.WriteLine("Error: Division by zero is not allowed.");
}
break;
default:
Console.WriteLine("Invalid choice.");
break;
}
}
}
static void GreetUser(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static double Add(double a, double b)
{
return a + b;
}
static double Subtract(double a, double b)
{
return a - b;
}
static double Multiply(double a, double b)
{
return a * b;
}
static double Divide(double a, double b)
{
if (b == 0) throw new DivideByZeroException();
return a / b;
}
}
}
csharpCopy code
using System;
namespace GuessingGame
{
class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.Start();
}
}
class Game
{
private int target;
private int attempts;
public Game()
{
Random rand = new Random();
this.target = rand.Next(1, 101);
this.attempts = 0;
}
public void Start()
{
Console.WriteLine("Welcome to the Guessing Game! Try to guess the number between 1 and 100.");
while (true)
{
Console.Write("Enter your guess: ");
int guess = Convert.ToInt32(Console.ReadLine());
this.attempts++;
GuessResult result = CheckGuess(guess);
if (result == GuessResult.Correct)
{
Console.WriteLine($"Congratulations! You've guessed the number {this.target} in {this.attempts} attempts.");
break;
}
else if (result == GuessResult.TooHigh)
{
Console.WriteLine("Your guess is too high. Try again!");
}
else