홍시홍의 프로그래밍

[C#] 간단한 Class 활용(제곱, 합 메서드 만들기) 본문

C#

[C#] 간단한 Class 활용(제곱, 합 메서드 만들기)

홍시홍 2017. 4. 5. 11:26

test 클라스 안에
power라는 제곱을 구하는 메서드와
sumall이라는 숫자의 합을 구하는 메서드


class Test 
    { 

        public int Power( int input) 
        { 
            return input * input; 

        } 
        public int Power(int input, int count) 
        { 
            int tot=1; 
            for( int i = 0; i < count; i++) 
            { 
                tot *= input; 
            } 
            return tot; 

        } 
        public int Sumall(int end) 
        { 
            int tot = 0; 
            for (int i = 0; i < end; i++) 
            { 
                tot += i; 
            } 
            return tot; 

        } 
        public int Sumall(int start, int end) 
        { 
            int tot = 0; 
            for (int i = start; i < end; i++) 
            { 
                tot += i; 
            } 
            return tot; 

        } 

    } 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            Test ad = new Test(); 
            Console.WriteLine(ad.Power(3)); 
            Console.WriteLine(ad.Power(3, 4)); 
            Console.WriteLine(ad.Sumall(3)); 
            Console.WriteLine(ad.Sumall(3, 12));  

        } 
    }
실행결과








Comments