C#

내일배움 C# 달리기반 Lv2 문제3,4

hunhun4949 2024. 8. 19. 20:42

3. 팩토리얼 출력하기

using System;
using System.Numerics;
using static System.Console;

namespace Practice1
{
    internal class Factorial
    {
        static void Main(string[] args)
        {
            int num;
            int result = 1;
            Write("Enter a number : ");
            num = int.Parse(ReadLine());

            for(int i=num; i>0; i--)
            {
                result *= i;
            }
            WriteLine($"Factorial of {num} is {result}");
        }
    }
}

 

4. 숫자 맞추기 게임

using System;
using System.Numerics;
using static System.Console;


namespace Practice1
{
    internal class Practice
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            int min = 1;
            int max = 100;
            int targetNumber = random.Next(min, max + 1);

            while(true)
            {
                Write($"Ender your guess ({min}-{max}): ");
                int temp = int.Parse(ReadLine());

                if (temp == targetNumber)
                {
                    WriteLine("Congratulation! You Guessed the number.");
                    break;
                }
                else if (temp < targetNumber) 
                {
                    WriteLine("Too low! Try again.");
                }
                else
                {
                    WriteLine("Too high! Try again.");
                }
            }
        }
    }
}