본문 바로가기

C#/기본 문법

C# 간단한 연습문제

using System;

namespace Rookiss_CSharp
{
    // [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문
    // 29 연습문제
    // https://www.inflearn.com/course/%EC%9C%A0%EB%8B%88%ED%8B%B0-mmorpg-%EA%B0%9C%EB%B0%9C-part1

    class Program
    {
        // 팩토리얼 : 재귀함수 버젼
        static int Factorial(int n)
        {
            if (n <= 1)
                return 1;

            return n * Factorial(n - 1);
        }

        // 팩토리얼 : 반복문
        /*
        static int Factorial(int n)
        {
            int ret = 1;

            for (int num = 1; num <= n; num++)
            {
                ret *= num;
            }

            return ret;
        }
        */

        static void Main(string[] args)
        {
            // 구구단
            for (int i = 2; i <= 9; i++)
            {
                for (int j = 1; j <= 9; j++)
                {
                    Console.WriteLine($"{i} x {j} = {i * j}");
                }
            }

            // 별찍기
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.Write("\n");
            }

            // 5! = 5 * 4 * 3 * 2 * 1
            // n! = n * (n-1) * ... * 1 (n>=1)
            int ret = Factorial(5);
            Console.WriteLine(ret);
        }
    }
}