daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Dec 12th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5.  
  6.  
  7. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  8.  
  9. int nod(int, int);
  10. int nok(int, int, int nod);
  11. int nokForN(int*, int);
  12. int nodForN(int*, int);
  13.  
  14. int main(int argc, char *argv[]) {
  15.  
  16.     int n, i, j, k;
  17.     int nodN, nokN;
  18.     AGAIN:
  19.     printf("Enter number of numbers\n");
  20.     i = scanf("%d", &n);
  21.     if (i == 0){
  22.         fflush(stdin);
  23.         printf("Mistake!, try again\n");
  24.         goto AGAIN;
  25.     }
  26.     int *mass;
  27.     mass = (int *)malloc(n * sizeof(int));
  28.     j = 0;
  29.     k = 0;
  30.     printf("Enter elements\n");
  31.     while (j < n){
  32.         fflush(stdin);
  33.         if (k > 0) printf("Enter elements correctly!\n");
  34.         for (i = 0; i < n; i++){
  35.             j += scanf("%d", &mass[i]);
  36.         }
  37.         k++;
  38.     }
  39.     nodN = nodForN(mass, n);
  40.     nokN = nokForN(mass, n);
  41.     printf("NOD = %d\nNOK = %d\nPress y to try again\n", nodN, nokN);
  42.     char y = getche();
  43.     if (y == 'y')
  44.     {
  45.         printf("\n");
  46.         goto AGAIN;
  47.     }
  48.     return 0;
  49. }
  50.  
  51. int nod(int a, int b)
  52. {
  53.     int temp;
  54.     if (a%b == 0) return b;
  55.     else return nod(b, a%b);
  56. }
  57.  
  58. int nok(int a, int b, int nod)
  59. {
  60.     return a*b/nod;
  61. }
  62.  
  63. int nodForN(int *mass, int n)
  64. {
  65.     int i;
  66.     int nodd = nod(mass[0], mass[1]);
  67.     if (n == 2) return nodd;
  68.     for (i = 2; i < n; ++i)
  69.     {
  70.         nodd = nod(nodd, mass[i]);
  71.     }
  72.     return nodd;
  73. }
  74.  
  75. int nokForN(int *mass, int n)
  76. {
  77.     int i;
  78.     int nokk = nok(mass[0], mass[1], nod(mass[0], mass[1]));
  79.     for (i = 2; i < n; ++i)
  80.     {
  81.         nokk = nok(nokk, mass[i], nod(nokk, mass[i]));
  82.     }
  83.     return nokk;   
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top