in Algorithms recategorized by
2,910 views
0 votes
0 votes

Assume $A$ and $B$ are non-zero positive integers. The following code segment:

while(A!=B){
    if*(A> B)
    A -= B;
    else
    B -= A;
}
cout<<A; // printing the value of A
  1. Computes the $LCM$ of two numbers
  2. Divides the larger number by the smaller number
  3. Computes the $GCD$ of two numbers
  4. Finds the smaller of two numbers
in Algorithms recategorized by
by
2441 3624 5537
2.9k views

4 Comments

it computes the GCD of two numbers so, option C is correct.
1
1

What is the significance of "*" after if statement?

1
1

@Sayan Bose

Can you explain significance of "*"?

0
0
Typing mistake
1
1

Subscribe to GO Classes for GATE CSE 2022

2 Answers

2 votes
2 votes
 
Best answer
This is Euclidean algo for determining HCF of two numbers which works in this way :

If we subtract smaller number from larger (we reduce larger number), GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
selected by
by
34 51 77

1 comment

it can be solved, by taking suitable numbers, such that each option gets a unique answer.

eg: A = 8, B = 12

option A) gives LCM of two numbers = 24

option B) if you divide a larger number by a smaller number it gives = 1

option C) GCD of two numbers = 4

option D) smaller of two numbers  = 8

now if you run the code by taking A = 8 and B=12, it gives the output as 4, which is GCD of two numbers.

0
0
3 votes
3 votes
option C is correct.
by
3 6 20
Answer:

Related questions