C언어 Express 9장 #08

SEUNGJUN JEONG·2022년 4월 16일
0

C/C++

목록 보기
1/15

재귀를 사용하여 푸는 문제이다.

#include <stdio.h>

int count_num(int number) {
    static int count = 1; // 정적변수로 자릿수 세는거

    if (number / 10 != 0) { // 나머지 0 나올 때 까지 나눠줌
        count++;
        count_num(number / 10); // 정수값 나눠서 재귀
    }
    return count; // 나머지 0 나오면 자릿수 돌려보냄
}

int main() {
    int num;
    printf("정수를 입력하시오: ");
    scanf("%d", &num);

    printf("자리수의 개수: %d", count_num(num)); // 정수 입력받고 자릿수 세는거로 보냄 -> 자릿수 얼만지 받음

    return 0;
}
profile
Microsoft Learn Student Ambassadors

0개의 댓글