happyguy89

IPE 3C

Jun 23rd, 2021
628
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ----------find GCD-----------
  2. #include <stdio.h>
  3. int gcd(int x, int y)
  4. {
  5.     while (x %= y)
  6.     {
  7.         int t = x;
  8.         x = y;
  9.         y = t;
  10.     }
  11.     return y;
  12. }
  13. int main()
  14. {
  15.     int x = 8, y = 16, z = 6;
  16.     int ans = gcd(gcd(x, y), z);
  17.     printf("%d",ans);
  18.     return 0;
  19. }
  20.  
  21. ------------find LCM-------------
  22. #include<stdio.h>
  23. int max(int a,int b,int c)  //Custom function max
  24. {
  25.   if(a>b&&a>c)            // If the value of a is the largest
  26.       return a;           // return a
  27.   else if(b>a&&b>c)       // If the value of b is the largest
  28.       return b;           // return b
  29.   else
  30.       return c;           // otherwise return c
  31. }
  32. void main()
  33. {
  34.   int x,y,z,i,j,k;
  35.   printf("Please enter three numbers:");
  36.   scanf("%d%d%d",&x,&y,&z); // Enter three numbers from the keyboard
  37.   k = max(x,y,z);          //Call the max function to find the maximum value
  38.   i= 1;
  39.   while(1)                 //while loop, the condition value is 1
  40.   {
  41.     j = k * i;
  42.     if((j%x==0)&&(j%y==0)&&(j%z==0)) //Satisfy the conditions, exit the loop
  43.         break;
  44.     i++;
  45.   }
  46.   printf("The least common multiple is %d\n",j);
  47. }
  48.  
RAW Paste Data