adnanj

Brojevi koji su jednaki sumi kubova svojih cifri

Aug 31st, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool suma (int);
  5. void ispis (int);
  6.  
  7. int main() {
  8.     int n;
  9.  
  10.     do {
  11.        cout << "Unesite prirodan broj n (ne veci od 200): ";
  12.        cin >> n;
  13.     } while(n <= 0 || n > 200);
  14.  
  15.     cout << "Brojevi koji su jednaki sumi kubova svojih cifri su: ";
  16.     ispis(n);
  17.  
  18.     system("pause>0");
  19.     return 0;
  20. }
  21.  
  22. bool suma (int broj) {
  23.     int suma = 0, tempBroj = broj;
  24.  
  25.     while(broj > 0) {
  26.         suma += pow(broj%10, 3);
  27.         broj /= 10;
  28.     }
  29.  
  30.     if(suma == tempBroj) return true;
  31.     return false;
  32. }
  33.  
  34. void ispis (int n) {
  35.     for(int i = 1; i <= n; i++)
  36.         if(suma(i))
  37.            cout << i << endl;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment