Advertisement
adnanj

Ispis srednje cifre broja

Dec 6th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool provjera(int);
  5. int getSrednjaCifra(int);
  6.  
  7. int main() {
  8.         int broj;
  9.  
  10.     do {
  11.     cout << "Unesite prirodan broj n: ";
  12.     cin >> broj;
  13.     }
  14.     while(broj <= 0);
  15.  
  16.     if(provjera(broj))
  17.             cout << "Srednja znamenka je " << getSrednjaCifra(broj) << ".";
  18.     else
  19.         cout << "Uneseni broj nema srednju znamenku!";
  20.  
  21.     system("pause>0");
  22.     return 0;
  23. }
  24.  
  25. bool provjera(int broj) {
  26.         if(broj < 1000 || broj > 9999)
  27.        return false;
  28.         return true;
  29. }
  30.  
  31. int getSrednjaCifra(int broj) {
  32.         int temp = broj, cifre = 0;
  33.     while(temp) {
  34.         temp /= 10;
  35.         cifre++;
  36.     }
  37.     return int(broj / pow(10.0, cifre/2)) % 10;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement