Problem solving/Algorithms

[프로그래머스]약수의 합 (C#)

Young_A 2025. 7. 8. 20:41

목차

    프로그래머스 - 약수의 합

     

    프로그래머스

    SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

    programmers.co.kr

    Problem Description

    정수 n을 입력받아 n의 약수를 모두 더한 값을 리턴하는 함수, solution을 완성해주세요.

    Example:

    n return
    12 28
    5 6

     

    Constraints:

    n은 0 이상 3000이하인 정수입니다.

     

    My Solution (C#)

    public static int Solution(int n)
    {
        int answer = 0;
    
        for(int i = 1; i <= n; i++)
        {
            if (n % i == 0)
                answer += i;
        }
    
        return answer;
    }