silentkiler029

Average of the digits (modular arithmetic)

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