michael_hartman_cz

PedF UK - OKB1319205 Ukol2

May 21st, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #define NUMBERS_COUNT 10
  3.  
  4.  /**
  5.   * Function which determines whether or not is one number divisible by another.
  6.   *
  7.   * @param dividend Number we want to know if is divisible.
  8.   * @param divisor Number by which we want to examine divisibility.
  9.   * @return Returns true if is divisible otherwise false.
  10.   */
  11.  
  12. bool isDivisibleBy ( int dividend, int divisor )
  13.  {
  14.     return ! ( dividend % divisor );
  15.  }
  16.  
  17.  /**
  18.   * Program which asks for ten numbers and then prints sum of which of them are divisible by 3, 5 and 7.
  19.   *
  20.   * @return Returns 1 if wrong input is detected, 0 if everything ok.
  21.   * @author Michael Hartman [email protected]
  22.   */
  23.  
  24. int main ( int argc, char ** argv )
  25.  {    
  26.     std::cout << "Enter 10 numbers: ";
  27.     int divisibleBy3 = 0, divisibleBy5 = 0, divisibleBy7 = 0, tmp;
  28.     for ( int i = 0; i < NUMBERS_COUNT; ++ i )
  29.      {
  30.         if ( ! ( std::cin >> tmp ) )
  31.          {
  32.             std::cerr << "Wrong input!" << std::endl;
  33.             return 1;
  34.          }
  35.  
  36.         if ( isDivisibleBy ( tmp, 3 ) )
  37.             divisibleBy3 += tmp;
  38.  
  39.         if ( isDivisibleBy ( tmp, 5 ) )
  40.             divisibleBy5 += tmp;
  41.  
  42.         if ( isDivisibleBy ( tmp, 7 ) )
  43.             divisibleBy7 += tmp;        
  44.      }
  45.  
  46.     std::cout << "soucet delitelnych tremi = " << divisibleBy3 << std::endl;
  47.     std::cout << "soucet delitelnych peti = " << divisibleBy5 << std::endl;
  48.     std::cout << "soucet delitelnych sedmi = " << divisibleBy7 << std::endl;
  49.     return 0;
  50.  }
Advertisement
Add Comment
Please, Sign In to add comment