.NET C# Lab

                        
using System;

class Program
{
    static void Main()
    {
        // Prompt the user for input and read three numbers
        Console.Write("Enter the first number: ");
        int num1 = int.Parse(Console.ReadLine());

        Console.Write("Enter the second number: ");
        int num2 = int.Parse(Console.ReadLine());

        Console.Write("Enter the third number: ");
        int num3 = int.Parse(Console.ReadLine());

        // Find the largest number using the ternary operator
        int largest = (num1 > num2)
                        ? ((num1 > num3) ? num1 : num3)
                        : ((num2 > num3) ? num2 : num3);

        // Output the largest number
        Console.WriteLine("The largest number is: " + largest);
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Prompt the user for input
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());

        // Calculate the number of digits
        int originalNumber = number;
        int numberOfDigits = number.ToString().Length;
        int sum = 0;

        // Calculate the sum of each digit raised to the power of the number of digits
        while (number > 0)
        {
            int digit = number % 10;
            sum += (int)Math.Pow(digit, numberOfDigits);
            number /= 10;
        }

        // Check if the number is an Armstrong number
        if (sum == originalNumber)
        {
            Console.WriteLine(originalNumber + " is an Armstrong number.");
        }
        else
        {
            Console.WriteLine(originalNumber + " is not an Armstrong number.");
        }
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Prompt the user for input
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());

        // Variable to store the reversed number
        int reversedNumber = 0;

        // Reverse the number
        while (number > 0)
        {
            int digit = number % 10; // Get the last digit
            reversedNumber = (reversedNumber * 10) + digit; // Append digit to reversed number
            number /= 10; // Remove the last digit from the original number
        }

        // Output the reversed number
        Console.WriteLine("The reversed number is: " + reversedNumber);
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Prompt the user for input
        Console.Write("Enter a number to print its table: ");
        int number = int.Parse(Console.ReadLine());

        // Print the multiplication table
        Console.WriteLine("Multiplication table for " + number + ":");
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(number + " x " + i + " = " + (number * i));
        }
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Prompt the user for input
        Console.Write("Enter a number to find its factorial: ");
        int number = int.Parse(Console.ReadLine());

        // Call the Factorial function and store the result
        long result = Factorial(number);

        // Output the result
        Console.WriteLine("The factorial of " + number + " is: " + result);
    }

    // Function to calculate factorial
    static long Factorial(int n)
    {
        long factorial = 1;

        for (int i = 1; i <= n; i++)
        {
            factorial *= i; // Multiply factorial by current number
        }

        return factorial;
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Prompt the user for input
        Console.Write("Enter a number to check if it is prime: ");
        int number = int.Parse(Console.ReadLine());

        // Call the IsPrime function and store the result
        bool isPrime = IsPrime(number);

        // Output the result
        if (isPrime)
        {
            Console.WriteLine(number + " is a prime number.");
        }
        else
        {
            Console.WriteLine(number + " is not a prime number.");
        }
    }

    // Function to check if a number is prime
    static bool IsPrime(int n)
    {
        // Check for numbers less than 2
        if (n < 2)
            return false;

        // Check from 2 to the square root of n
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
                return false; // n is divisible by i, hence not prime
        }

        return true; // n is prime
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Input array size
        Console.Write("Enter the number of elements in the array: ");
        int size = int.Parse(Console.ReadLine());
        
        // Initialize the array
        int[] array = new int[size];
        
        // Input array elements
        Console.WriteLine("Enter the elements of the array:");
        for (int i = 0; i < size; i++)
        {
            Console.Write($"Element {i + 1}: ");
            array[i] = int.Parse(Console.ReadLine());
        }
        
        // Find max and min elements
        (int max, int min) = FindMaxAndMin(array);
        
        // Display results
        Console.WriteLine($"Maximum element: {max}");
        Console.WriteLine($"Minimum element: {min}");
    }
    
    static (int, int) FindMaxAndMin(int[] array)
    {
        if (array.Length == 0)
        {
            throw new ArgumentException("Array cannot be empty.");
        }
        
        int max = array[0];
        int min = array[0];
        
        foreach (int element in array)
        {
            if (element > max)
            {
                max = element;
            }
            if (element < min)
            {
                min = element;
            }
        }
        
        return (max, min);
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Example sorted array
        int[] sortedArray = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
        
        Console.Write("Enter the number to search: ");
        int target = int.Parse(Console.ReadLine());
        
        int index = BinarySearch(sortedArray, target);
        
        if (index != -1)
        {
            Console.WriteLine($"Number {target} found at index {index}.");
        }
        else
        {
            Console.WriteLine($"Number {target} not found in the array.");
        }
    }
    
    static int BinarySearch(int[] array, int target)
    {
        int left = 0;
        int right = array.Length - 1;
        
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            
            // Check if the target is at the mid index
            if (array[mid] == target)
            {
                return mid; // Target found
            }
            
            // If the target is greater, ignore the left half
            if (array[mid] < target)
            {
                left = mid + 1;
            }
            // If the target is smaller, ignore the right half
            else
            {
                right = mid - 1;
            }
        }
        
        // Target is not present in the array
        return -1;
    }
}
                        
                    
                        
using System;

class Program
{
    static void Main()
    {
        // Input array size
        Console.Write("Enter the number of elements in the array: ");
        int size = int.Parse(Console.ReadLine());
        
        // Initialize the array
        int[] array = new int[size];
        
        // Input array elements
        Console.WriteLine("Enter the elements of the array:");
        for (int i = 0; i < size; i++)
        {
            Console.Write($"Element {i + 1}: ");
            array[i] = int.Parse(Console.ReadLine());
        }
        
        // Reverse the array
        ReverseArray(array);
        
        // Display the reversed array
        Console.WriteLine("Reversed array:");
        foreach (int element in array)
        {
            Console.Write(element + " ");
        }
        Console.WriteLine();
    }
    
    static void ReverseArray(int[] array)
    {
        int left = 0;
        int right = array.Length - 1;
        
        while (left < right)
        {
            // Swap elements at left and right indices
            int temp = array[left];
            array[left] = array[right];
            array[right] = temp;
            
            // Move indices towards the center
            left++;
            right--;
        }
    }
}