Denis_Hristov

HomeWork C Day3

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