Advertisement
Lisaveta777

NOK-NOD

Dec 16th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. Pastebin
  2. //nod in array
  3.  
  4. #include <stdio.h>
  5. #include <math.h>
  6. #define SIZE 10
  7.  
  8. int nod(int d,int element);
  9.  
  10. int main()
  11. {
  12.      int i,arr[SIZE],divider;//
  13.      for(i=0;i<SIZE;i++)arr[i] = rand()%20+1;//populate array
  14.      for(i=0;i<SIZE;i++)printf("%d\t",arr[i]);//print array
  15.  
  16.      divider= arr[0];
  17.      for(i=1;i<SIZE;i++)divider = nod(divider,arr[i]);//
  18.  
  19.     return 0;
  20. }
  21. int nod(int div,int element)
  22. {
  23.     int nod,i=2;
  24.     nod =div*element;
  25.     printf("\nnow for %d and %d\n",div,element);
  26.  
  27.     while(i<=nod/2)
  28.     {
  29.         if(!(element%i)&&!(div%i))
  30.         {
  31.             printf("dividable i is %d\n",i);
  32.             element/=i;
  33.             div/=i;
  34.             nod/=i;
  35.             printf(" i-%d,element-%d,div-%d,nod-%d\n",i,element,div,nod);
  36.         }
  37.  
  38.         else     //there might be duplication of digit(2,2...) 24=2*2*2*3
  39.             i++;
  40.  
  41.     }
  42.     return nod;
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement