C.4 Product function

lsw·2021년 4월 15일
0

C

목록 보기
5/9
post-thumbnail

1. 내용

  1. recursion을 이용해 곱셈함수를 만든다.
  2. for ~ 문을 이용해 곱셈함수를 만든다.

2. 코드

recursion function(함수 재귀를 이용)

int product_1(int num1, int num2)
{
    static int total = 0;
    if (num2 == 0)
    {
        return total;
    }
    total += num1;
    product_1(num1, --num2);
}

for ~ function(for ~ 반복문을 이용)

int product_2(int num1, int num2)
{
    int total = 0;
    for (int i = 0; i < num2; i++)
    {
        total += num1;
    }
    return total;
}

main function

int main()
{
    int num1, num2;
    printf("Enter num1 and num2 : ");
    scanf_s("%d %d", &num1, &num2);
    printf("num1 x num2 by product_1= %d\n", product_1(num1, num2));
    printf("num1 x num2 by product_2= %d", product_2(num1, num2));
}

3. 결과


4. 결론

하나의 목적지, 여러개의 길

profile
미생 개발자

0개의 댓글