Workshop3 212802
Workshop3 212802
3
Workshop Exercise 1:
Create a method named "GetFullName" that takes two string
parameters, "firstName" and "lastName." The method should return
the full name by concatenating the first and last names with a space in
between. Handle the cases where either the first name or the last name
is empty and return the non-empty name. Test the method by calling it
with different arguments and printing the result.
using System;
class Program
{
static void Main()
{
// Test cases
Console.WriteLine(GetFullName("John", "Doe")); // Output: "John Doe"
Console.WriteLine(GetFullName("Jane", "")); // Output: "Jane"
Console.WriteLine(GetFullName("", "Smith")); // Output: "Smith"
Console.WriteLine(GetFullName("", "")); // Output: ""
// Additional tests
Console.WriteLine(GetFullName("Alice", "Johnson")); // Output: "Alice Johnson"
Console.WriteLine(GetFullName("", "Brown")); // Output: "Brown"
}
class Program
{
static void Main()
{
// Define the local function
int Add(int x, int y)
{
return x + y;
}
class Program
{
static void Main()
{
// Test cases for RecursiveFactorial
Console.WriteLine($"Factorial of 5: {RecursiveFactorial(5)}"); // Output: 120
Console.WriteLine($"Factorial of 7: {RecursiveFactorial(7)}"); // Output: 5040
Console.WriteLine($"Factorial of 0: {RecursiveFactorial(0)}"); // Output: 1
Console.WriteLine($"Factorial of 1: {RecursiveFactorial(1)}"); // Output: 1
}
class Program
{
static void Main()
{
// Test cases for CalculateSum
Console.WriteLine($"Sum of 5 and 3: {CalculateSum(5, 3)}"); // Output: 8
Console.WriteLine($"Sum of 10 and 20: {CalculateSum(10, 20)}"); // Output: 30
Console.WriteLine($"Sum of -5 and 15: {CalculateSum(-5, 15)}"); // Output: 10
Console.WriteLine($"Sum of 0 and 0: {CalculateSum(0, 0)}"); // Output: 0
}
class Program
{
static void Main()
{
// Test cases for PrintMultiples
PrintMultiples(3); // Output: 3, 6, 9, 12, 15
PrintMultiples(5); // Output: 5, 10, 15, 20, 25
PrintMultiples(7); // Output: 7, 14, 21, 28, 35
PrintMultiples(1); // Output: 1, 2, 3, 4, 5
}
class Program
{
static void Main()
{
// Calling the method with only the required parameter
DisplayMessage("Hello");
// Calling the method with both the required and optional parameters
DisplayMessage("Hello", "World");
}
class Program
{
static void Main()
{
// Correct usage: All positional arguments come before named arguments
PrintDetails("John", 30, city: "New York");
// Perform withdrawal
}
}
output:
Unhandled exception. System.InvalidOperationException: Cannot perform operations on a closed account.
Workshop Exercise:
Explain the concept of recursion in methods. Provide an example of a
recursive method in C#.
using System;
class Program
{
static void Main()
{
// Test cases for RecursiveFactorial
Console.WriteLine($"Factorial of 5: {RecursiveFactorial(5)}"); // Output: 120
Console.WriteLine($"Factorial of 7: {RecursiveFactorial(7)}"); // Output: 5040
Console.WriteLine($"Factorial of 0: {RecursiveFactorial(0)}"); // Output: 1
}
class Program
{
static void Main()
{
// Test the Divide method with different inputs
DivideNumbers(10, 2); // Valid division
DivideNumbers(10, 0); // Division by zero
}
using System;
class Program
int RecursiveFactorial(int n)
if (n < 2)
return 1;
}
Conrolling Parametrs:
using System;
class Program
x++;
y++;
z++;
int a = 10;
int b = 20;