TsetsoP

day3

Jul 8th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. //zad1//
  2. int isPrime(int n)
  3. {
  4. if (n == 2 || n == 3 || n == 5 || n == 7)
  5. {
  6. return 1;
  7. }
  8. if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0)
  9. {
  10. return 0;
  11. }
  12. return 1;
  13. }
  14. int main()
  15. {
  16.  
  17. printf("%s\n", isPrime(15) ? "Yes" : "No");
  18.  
  19.  
  20. return 0;
  21. }
  22. ---------------------------------------------------
  23. //zad2//
  24. void Eratosten(int n){
  25.  
  26. int numbers[n];
  27. numbers[0] = 1;
  28. numbers[1] = 1;
  29.  
  30. int start = 2;
  31.  
  32. while(start <= 7){
  33. if(numbers[start] == 1){
  34. start++;
  35. continue;
  36. }
  37.  
  38. numbers[start] = 0;
  39.  
  40. for(int i = start * 2; i <= n; i += start){
  41. numbers[i] = 1;
  42. }
  43.  
  44. start++;
  45. }
  46.  
  47. for(int i = 0; i < n; i++){
  48. if(numbers[i] == 0){
  49. printf("%d\n", i);
  50. }
  51. }
  52. }
  53. int main ()
  54. {
  55. Eratosthen(100);
  56. return 0;
  57. }
  58.  
  59. -----------------------------------------
  60. //zad3//
  61. void BubbleSort(int arr[], int n){
  62.  
  63. int temp;
  64.  
  65. for(int i = 0 ; i < n - 1; i++){
  66. for(int j = 0 ; j < n - i - 1; j++){
  67. if(arr[j] > arr[j+1]){
  68. temp = arr[j];
  69. arr[j] = arr[j+1];
  70. arr[j+1] = temp;
  71. }
  72. }
  73. }
  74. }
  75. int main ()
  76. {
  77. int arr[] = {5, 12, 3, 21, 43};
  78. BubbleSort(arr, 5);
  79. for(int i = 0; i < 5; i++){
  80. printf("%d\n", arr[i]);
  81. }
  82. return 0;
  83. }
  84.  
  85. ---------------------------------------------
  86. //zad4//
  87. int factorial(int n){
  88. if (n == 0){
  89. return 1;
  90. }
  91. return factorial(n - 1) * n;
  92. }
  93. int main ()
  94. {
  95. printf("%d\n", factorial(7));
  96. return 0;
  97. }
  98. --------------------------------------------
  99. //zad5//
  100. int Fibonachi(int* arr, int n, int start){
  101. arr[0] = 0;
  102. arr[1] = 1;
  103.  
  104. arr[start] = arr[start - 1] + arr[start - 2];
  105.  
  106. if(start <= n){
  107. Fibonachi(arr, n, start + 1);
  108. }
  109.  
  110. return arr[n - 1];
  111. }
  112. int main ()
  113. {
  114. int n = 8;
  115. int arr[n];
  116. printf("%d\n", Fibonachi(arr, n, 2));
  117. return 0;
  118. }
  119. -------------------------------------------
  120. //zad6//
  121. int power(int n1, int n2);
  122.  
  123. int power(int a, int b) {
  124. if (b != 0){
  125. return (a * power(a, b - 1));
  126. }else{
  127. return 1;
  128. }
  129. }
  130. int main ()
  131. {
  132. int a, b, result;
  133.  
  134. printf("Enter number: ");
  135. scanf("%d", &a);
  136.  
  137. printf("Enter power: ");
  138. scanf("%d", &b);
  139.  
  140. result = power(a, b);
  141.  
  142. printf("%d", result);
  143. return 0;
  144.  
  145. }
  146. ------------------------------------------
  147. //zadd8//
  148. double sumArr(double* arr, int size, int start){
  149. if (start == size){
  150. return 0;
  151. }
  152. return arr[start] + sumArr(arr, size, start + 1);
  153. }
  154. int main()
  155. {
  156. double arr[] = {1, 6, 12, 16, 3, 5};
  157. printf("%f\n", sumArr(arr, 6, 0));
  158. return 0;
  159. }
  160.  
  161.  
  162.  
  163.  
Add Comment
Please, Sign In to add comment