Advertisement
Kacper_Michalak

Zadania Funkcje

Feb 1st, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. /* Zadanie 1*/
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int SUMA(int x, int y)
  6. {
  7.     int wynik = 0;
  8.  
  9.     wynik = x + y;
  10.  
  11.     return wynik;
  12. }
  13.  
  14.  
  15. int main()
  16. {
  17.     cout << SUMA(5, 6) << endl;
  18.     cout << SUMA(54, 67) << endl;
  19.     cout << SUMA(55, 60) << endl;
  20. }
  21. /* Zadanie 2 */
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int WARTOSC_BEZWZGLEDNA(int x)
  26. {
  27.     if (x < 0)
  28.     {
  29.         x = x * -1;
  30.     }
  31.     return x;
  32. }
  33.  
  34. int main()
  35. {
  36.     cout << WARTOSC_BEZWZGLEDNA(-5)<<endl;
  37.     cout << WARTOSC_BEZWZGLEDNA(10)<<endl;
  38.     cout << WARTOSC_BEZWZGLEDNA(-99)<<endl;
  39. }
  40. /* Zadanie 3 */
  41. #include <iostream>
  42. using namespace std;
  43.  
  44. bool CZY_PARZYSTA(int x)
  45. {
  46.     if (x % 2 == 0)
  47.     {
  48.         return true;
  49.     }
  50.     else
  51.     {
  52.         return false;
  53.     }
  54. }
  55.  
  56. int main()
  57. {
  58.     cout << CZY_PARZYSTA(-5)<<endl;
  59.     cout << CZY_PARZYSTA(10)<<endl;
  60.     cout << CZY_PARZYSTA(-99)<<endl;
  61. }
  62. /* Zadanie 4 */
  63. #include <iostream>
  64. using namespace std;
  65.  
  66. void SZLACZEK(int x, char y)
  67. {
  68.     for (int i = 0; i < x; i++)
  69.     {
  70.         cout << y;
  71.     }
  72.     cout << "\n";
  73. }
  74.  
  75. int main()
  76. {
  77.     SZLACZEK(5, '*');
  78.     SZLACZEK(10, '$');
  79.     SZLACZEK(3, '^');
  80.    
  81. }
  82. /* Zadanie 5 */
  83. #include <iostream>
  84. using namespace std;
  85.  
  86. int POTEGA(int x, int y)
  87. {
  88.     int wynik=1;
  89.     if (y == 0)
  90.     {
  91.         return 1;
  92.     }
  93.     else
  94.     {
  95.         for (int i = 0; i < y; i++)
  96.         {
  97.             wynik = wynik * x;
  98.         }
  99.         return wynik;
  100.     }
  101.    
  102.  
  103. }
  104.  
  105. int main()
  106. {
  107.     cout << POTEGA(5, 4) << endl;
  108.     cout << POTEGA(10, 2) << endl;
  109.     cout << POTEGA(3, 1) << endl;
  110.  
  111. }
  112. /* Zadanie 6 */
  113. #include <iostream>
  114. using namespace std;
  115.  
  116. int OBWOD_TROJKATA(int x, int y, int z)
  117. {
  118.     int wynik = 0;
  119.  
  120.     if ((x + y > z) && (x + z > y) && (z + y > x))
  121.     {
  122.         wynik = x + y + z;
  123.         return wynik;
  124.     }
  125.     else
  126.     {
  127.         return -1;
  128.     }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.     if (OBWOD_TROJKATA(5, 3, 7) > 0)
  135.     {
  136.         cout << OBWOD_TROJKATA(5, 3, 7) << endl;
  137.     }
  138.     else cout << "Nie mozna zbudowac takiego trojkata" << endl;
  139.     if (OBWOD_TROJKATA(10, 2, 2) > 0)
  140.     {
  141.         cout << OBWOD_TROJKATA(10, 2, 2) << endl;
  142.     }
  143.     else cout << "Nie mozna zbudowac takiego trojkata" << endl;
  144.     if (OBWOD_TROJKATA(3, 1, 3) > 0)
  145.     {
  146.         cout << OBWOD_TROJKATA(3, 1, 3) << endl;
  147.     }
  148.     else cout << "Nie mozna zbudowac takiego trojkata" << endl;
  149. }
  150. /* Zadanie 7 */
  151. #include <iostream>
  152. using namespace std;
  153.  
  154. int SILNIA(int x)
  155. {
  156.     if (x < 2)
  157.     {
  158.         return 1;
  159.     }
  160.     return x * SILNIA(x - 1);
  161. }
  162.  
  163. int main()
  164. {
  165.     cout << SILNIA(5) << endl;
  166.     cout << SILNIA(2) << endl;
  167.     cout << SILNIA(9) << endl;
  168. }
  169. /* Zadanie 8 */
  170. #include <iostream>
  171. using namespace std;
  172.  
  173. int MAX (int x, int y, int z)
  174. {
  175.     if (x > y && x > z)
  176.     {
  177.         return x;
  178.     }
  179.     else if (y > x && y > z)
  180.     {
  181.         return y;
  182.     }
  183.     else return z;
  184. }
  185.  
  186. int main()
  187. {
  188.     cout << MAX(4, 3, 2) << endl;
  189.     cout << MAX(5, 8, 1) << endl;
  190.     cout << MAX(1, 7, 10) << endl;
  191. }
  192. /* Zadanie 9 */
  193. #include <iostream>
  194. using namespace std;
  195.  
  196. void DZIELNIKI (int x)
  197. {
  198.     for (int i = 1; i <= x; i++)
  199.     {
  200.         if (x % i == 0)
  201.         {
  202.             cout << i << " ";
  203.         }
  204.     }
  205.     cout << "\n";
  206. }
  207.  
  208. int main()
  209. {
  210.     DZIELNIKI(5);
  211.     DZIELNIKI(9);
  212.     DZIELNIKI(13);
  213. }
  214. /* Zadanie 10 */
  215. #include <iostream>
  216. using namespace std;
  217.  
  218. double ILORAZ(double x, double y)
  219. {
  220.     double wynik=0;
  221.     if (x >= 0 && y >= 0)
  222.     {
  223.         if (y != 0)
  224.         {
  225.             wynik = x / y;
  226.             return wynik;
  227.         }
  228.         else return -1;
  229.     }
  230.     else return -2;
  231. }
  232.  
  233. int main()
  234. {
  235.     if (ILORAZ(4, 0) == -2)
  236.     {
  237.         cout << "Nie obsluguje ujemnych" << endl;
  238.     }
  239.     else if (ILORAZ(4, 0) == -1)
  240.     {
  241.         cout << "Nie dziel przez 0" << endl;
  242.     }
  243.     else cout << "wynik z dzielenia wynosi: " << ILORAZ(4, 2) << endl;
  244.  
  245.     if (ILORAZ(5, 3) == -2)
  246.     {
  247.         cout << "Nie obsluguje ujemnych" << endl;
  248.     }
  249.     else if (ILORAZ(5, 3) == -1)
  250.     {
  251.         cout << "Nie dziel przez 0" << endl;
  252.     }
  253.     else cout << "wynik z dzielenia wynosi: " << ILORAZ(5, 3) << endl;
  254.  
  255.     if (ILORAZ(4, -8) == -2)
  256.     {
  257.         cout << "Nie obsluguje ujemnych" << endl;
  258.     }
  259.     else if (ILORAZ(4, -8) == -1)
  260.     {
  261.         cout << "Nie dziel przez 0" << endl;
  262.     }
  263.     else cout << "wynik z dzielenia wynosi: " << ILORAZ(4, -8) << endl;
  264. }
  265. /* Zadanie 11 */
  266. #include <iostream>
  267. using namespace std;
  268.  
  269. void  PODZIELNOSC(int x, int y)
  270. {
  271.     int i=1;
  272.     int tymczasowa;
  273.    
  274.     if (x > 1)
  275.     {
  276.         for (int j = 0; j < x-1; j++)
  277.         {
  278.             i *= 10;
  279.         }
  280.        
  281.     }
  282.     tymczasowa = i * 10;
  283.  
  284.         do
  285.         {
  286.             if (i % y == 0)
  287.             {
  288.                 cout << i << " ";
  289.             }
  290.             i++;
  291.         } while (i < tymczasowa);
  292.    
  293.     cout << "\n";
  294. }
  295.  
  296. int main()
  297. {
  298.     PODZIELNOSC(1, 4);
  299.     PODZIELNOSC(2, 6);
  300.     PODZIELNOSC(3, 8);
  301.     PODZIELNOSC(4, 92);
  302.  
  303. }
  304. /* Zadanie 12 */
  305. #include <iostream>
  306. using namespace std;
  307.  
  308. int LICZBA_CYFR(long long x)
  309. {
  310.     int y = 0;
  311.     do
  312.     {
  313.         x = x / 10;
  314.         y++;
  315.     } while (x != 0);
  316.     return y;
  317. }
  318.  
  319. int main()
  320. {
  321.     cout << LICZBA_CYFR(1562156) << endl;
  322.     cout << LICZBA_CYFR(1562156455545) << endl;
  323.     cout << LICZBA_CYFR(1562156564561216544) << endl;
  324.     cout << LICZBA_CYFR(15621564654656464) << endl;
  325. }
  326. /* Zadanie 13 */
  327. #include <iostream>
  328. using namespace std;
  329.  
  330. int SUMA_CYFR(int &x)
  331. {
  332.     int suma = 0;
  333.     int y=0;
  334.  
  335.     do
  336.     {
  337.         y = x % 10;
  338.         suma +=y;
  339.         x = x / 10;
  340.     } while (x > 0);
  341.     x = suma;
  342.     return x;
  343. }
  344.  
  345. int main()
  346. {
  347.     int x;
  348.     cout << "Podaj liczbe: " << endl;
  349.     cin >> x;
  350.  
  351.     do {
  352.         SUMA_CYFR(x);
  353.     } while (SUMA_CYFR(x) > 10);
  354.  
  355.     cout << SUMA_CYFR(x);
  356. }
  357.  
  358. /* Zadanie 14 i 15 */
  359. #include <iostream>
  360. #include <math.h>
  361. using namespace std;
  362.  
  363. int POLE_TROJKATA(int x, int h)
  364. {
  365.     int wynik = 0;
  366.     wynik = (x * h)/2;
  367.    
  368.     return wynik;
  369. }
  370.  
  371. int POLE_TROJKATA(int x1, int y1, int x2, int y2, int x3, int y3)
  372. {
  373.     int P;
  374.  
  375.     P = (abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)))/2;
  376.     return P;
  377. }
  378.  
  379. int main()
  380. {
  381.     cout << POLE_TROJKATA(6, 2) << endl;
  382.  
  383.     cout << POLE_TROJKATA(2, 1, -5, 4, -1, -2) << endl;
  384.  
  385.  
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement