홍시홍의 프로그래밍

[C#] 1~100 까지 합 구하기 본문

C#

[C#] 1~100 까지 합 구하기

홍시홍 2017. 4. 4. 13:06

test 클래스에 sum 이라는 메서드를 만들어서 구한다



class Test

    {

        public void sum(int a)

        {

            int tot = 0;

            for (int i = 1; i <= a; i++)

            {

                tot += i;

            }

            Console.WriteLine("1~" + a + " 합은 " + tot);

        }

        

    }

    class Program

    {

        static void Main(string[] args)

        {

            Test t = new Test();

            t.sum(10);

            t.sum(100);

            

        }

  }


Comments