Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. int num; // 입력
  7. int sum = 0; // 자릿수 더할 변수
  8. int cnt = 0; // 반복횟수 == 자릿수
  9.  
  10. printf("정수 입력 : ");
  11. scanf("%d", &num);
  12.  
  13. while (num > 0)
  14. {
  15. // 123 % 10 == 3 | 12 % 10 == 2 | 1 % 10 == 1
  16. sum = sum + (num % 10);
  17. // 123 / 10 == 12 | 12 / 10 == 1 | 1 / 10 == 0
  18. num = num / 10;
  19. cnt++; // 1 | 2 | 3
  20. }
  21.  
  22. printf("%d자리\n", cnt);
  23. printf("각 자리의 합은 %d\n", sum);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement