Solutions For the C# Challenges

using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 100; i++)
        {
            string output = "";

            if (i % 3 == 0) output += "Fizz";
            if (i % 5 == 0) output += "Buzz";
            if (i % 7 == 0) output += "Zap";

            Console.WriteLine(output == "" ? i.ToString() : output);
        }
    }
}
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());
        bool isPrime = true;
        int smallestDivisor = -1;

        for (int i = 2; i <= Math.Sqrt(number); i++)
        {
            if (number % i == 0)
            {
                isPrime = false;
                smallestDivisor = i;
                break;
            }
        }

        if (isPrime && number > 1)
        {
            Console.WriteLine($"{number} is Prime.");
        }
        else
        {
            Console.WriteLine($"{number} is Not Prime. Smallest divisor: {smallestDivisor}");
        }
    }
}
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());
        long factorial = 1;

        for (int i = 1; i <= number; i++)
        {
            factorial *= i;
        }

        Console.WriteLine($"{number}! = {factorial}");
    }
}
using System;

class Program
{
    static void Main()
    {
        Random random = new Random();
        int secretNumber = random.Next(1, 51);
        int attempts = 5;

        Console.WriteLine("Guess the number (between 1 and 50)!");

        while (attempts > 0)
        {
            Console.Write($"You have {attempts} attempts left. Enter your guess: ");
            int guess = int.Parse(Console.ReadLine());

            if (guess == secretNumber)
            {
                Console.WriteLine("Congratulations! You guessed the number!");
                return;
            }
            else if (guess > secretNumber)
            {
                Console.WriteLine("Too High!");
            }
            else
            {
                Console.WriteLine("Too Low!");
            }

            attempts--;
        }

        Console.WriteLine($"Sorry, you ran out of attempts. The number was {secretNumber}.");
    }
}
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the number of Fibonacci terms to display: ");
        int n = int.Parse(Console.ReadLine());
        
        int first = 0, second = 1;

        Console.WriteLine("Fibonacci sequence:");
        for (int i = 0; i < n; i++)
        {
            Console.Write(first + " ");
            int next = first + second;
            first = second;
            second = next;
        }
    }
}

Leave a Comment