silentkiler029

Average of the digits (modular arithmetic+function)

Jul 1st, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double Average(long long n)
  4. {
  5.     long long r, q = n, s = 0, tot = 0;
  6.     while(q > 0) {
  7.         r = q % 10;
  8.         s += r;
  9.         q /= 10;
  10.         tot++;
  11.     }
  12.     return (double)s / (double)tot;
  13. }
  14.  
  15. int main()
  16. {
  17.     long long x; // as x <= 10^19
  18.     scanf("%lld", &x);
  19.  
  20.     double avg = Average(x);
  21.  
  22.     printf("%lf\n", avg);
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment