Thursday, May 2, 2019

Recursion 6 : GCD and LCM of 2 Numbers

  • Problem Description

    Write a program to compute GCD and LCM of 2 numbers using recursion.


    Input and Output Format:


    Input consists of 2 integers.

    Refer sample input and output for formatting specifications.

    All text in bold corresponds to input and the rest corresponds to output.
  • Test Case 1

    Input (stdin)
    14

    16

    Expected Output
    GCD=2

    LCM=112
  • Test Case 2

    Input (stdin)
    30

    40

    Expected Output
    GCD=10

    LCM=120
sOLUTION

#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;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...