본문 바로가기

C&C++ 개발

9. Sum of Digits of a five Digit number

요구사항 :
다섯자리의 숫자가 들어올 때 각 자릿수마다 값을 더한 합계를 출력한다
10씩 나누어 하라


기능설계 :
scanf로 값을 하나 받아옴
10씩 나누어 각 자릿수를 더해 합계값을 출력한다


코드 :
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    int i;
    int sum = 0 ;

    scanf("%d", &n);

    for(i=0; i<5; i++)
    {              
        sum += n%10;
        n = n/10;
    }           

    printf("%d\n",sum);
}


추측한 학습목표 :
for문을 어떻게 쓸거니?

'C&C++ 개발' 카테고리의 다른 글

11. Conditional Statements in C  (0) 2018.12.14
10. Bitwise Operators  (0) 2018.12.14
8. Boxes through a tunnel  (0) 2018.12.11
7. Digit Frequency  (0) 2018.12.10
6. For Loop in C  (0) 2018.12.08