Saturday, August 18, 2018

GCD AND LCM

  • Problem Description

    Two integers A and B are the inputs. Write a program to find GCD and LCM of A and B.

    Input: each line contains an integer A and B.

    Output: Display the GCD and LCM of A and B separated by space respectively
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int a, b, x, y,temp,GCD,LCM;
      scanf("%d",&x);
      scanf("%d",&y);
      a=x;
      b=y;
      while(b!=0)
      {
        temp=b;
        b=a%b;
        a=temp;
      }
      GCD=a;
      LCM=(x*y)/GCD;
      printf("GCD=%d\n",GCD);
      printf("LCM=%d\n",LCM);
      return 0;
    }
      
  • Test Case 1

    Input (stdin)
    30 40
    
    
    Expected Output
    GCD=10
    
    LCM=120
  • Test Case 2

    Input (stdin)
    50 34
    
    
    Expected Output
    GCD=2
    
    LCM=850

No comments:

Post a Comment