Advertisement
aed1oN

Untitled

Sep 22nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void Ocisti () {
  4.     char x = 'a';
  5.     while(x != '\n')
  6.         scanf("%c", &x);
  7. }
  8. double Unesi () {
  9.     double broj;
  10.     scanf("%lf", &broj);
  11.     return broj;
  12. }
  13. double Prosjek (int broj) {
  14.     int brojac = 0, suma = 0, c;
  15.     double prosjek;
  16.     while(broj != 0) {
  17.          c = broj % 10;
  18.          brojac++;
  19.          suma += c;
  20.          broj /= 10;
  21.     }
  22.     prosjek = (double)suma / brojac;
  23.     return prosjek;
  24. }
  25.  
  26. int main() {
  27.     double x, d;
  28.     int cijeli;
  29.     do {
  30.         printf("Unesite cijeli broj: ");
  31.         x = Unesi();
  32.         if(x != (int)x) {
  33.             Ocisti();
  34.             printf("Unos pogresan!\n");
  35.         }
  36.     } while(x != (int)x);
  37.     cijeli = x;
  38.     if(cijeli == 0) {
  39.         printf("Prosjek je: %d", cijeli);
  40.         return 0;
  41.     }
  42.     d = Prosjek(cijeli);
  43.     printf("Prosjek je: %g", d);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement