Advertisement
kamandos

HomeWork

Jul 16th, 2022
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int sumOfDigits(int x);
  4. int sumOfDigitsFrom1ToN(int n);
  5.  
  6. int sumOfDigitsFrom1ToN(int n)
  7. {
  8.     int result = 0;  
  9.  
  10.     for (int x = 1; x <= n; x++)
  11.         result += sumOfDigits(x);
  12.  
  13.     return result;
  14. }
  15.  
  16. int sumOfDigits(int x)
  17. {
  18.     int sum = 0;
  19.     while (x != 0)
  20.     {
  21.         sum += x %10;
  22.         x   = x /10;
  23.     }
  24.     return sum;
  25. }
  26.  
  27. int main()
  28. {
  29.     int n = 328;
  30.      printf(" %d: %d",n,sumOfDigitsFrom1ToN(n));
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement