-1

below is my code for calculating gcd and lcm of two numbers. When i try with different test cases it works fine. But when i try to submit it on online platform it says wrong

package javapractice;
import java.util.*;
public class chef_gcdlcm {

    public static void main(String[] args) {
        try{
            Scanner input = new Scanner(System.in);
            long t = input.nextInt();
            long l,s,temp;
            while(t-->0){
                long A = input.nextLong();
                long B = input.nextLong();
                temp = 1;
                if(A>B){ l = A;
                 s = B;}
                else{ l = B;
                s = A;              
                }
                while(temp!=0){
                    temp = l%s;
                    if(temp!=0){
                        if(temp>s){l = temp;}
                        else{s = temp;}
                    }   
                }
                long gcd = (A*B)/s;
                System.out.println(s +" "+gcd);
            }
            input.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
RAP
  • 21
  • 6

2 Answers2

1

your code doesn't work, here's fixed;

package javapractice;
import java.util.*;
public class chef_gcdlcm {

  public static void main(String[] args) {
    try{
        Scanner input = new Scanner(System.in);
        long t = input.nextInt();
        long l,s,temp;
        while(t-->0){
            long A = input.nextLong();
            long B = input.nextLong();
            temp = 1;
            if(A>B){ 
               l = A;
               s = B;
            }else{ l = B;
               s = A;              
            }
            while (s != 0) {
              temp = l % s;
              if (temp > s) {
                 l = temp;
              } else {
                 l = s;
                 s = temp;
              }
            }
           long gcd = (A * B) / l;
           System.out.println(l + ":" + gcd);
        }
        input.close();
    }catch(Exception e){
        e.printStackTrace();
    }

  }

}

what you missed;

  1. you forgot set large to small (l=s) when temp is smaller than l.
  2. you don't have to check this: if(temp!=0) the while loop takes care of that.
  3. you should divide A*B to l, not to s at the end. s should come out 0.
emrhzc
  • 1,347
  • 11
  • 19
0

It isn't necessary to do the swapping and comparing. If necessary, the values will be corrected after the first iteration.

Consider l = 24 and s = 36
save = s;    save is 36
l %= s;      l is still 24 
s = l;       s is now 24
l = save     l is now 36

Modified code

try {
    Scanner input = new Scanner(System.in);
    long t = input.nextInt();
    long l, s, temp;
    while (t-- > 0) {
        l = input.nextLong();
        s = input.nextLong();
        long A = s;
        long B = l;
        // doesn't matter which is larger.  It will be corrected
        // after the first iteration.
        while (s > 0) {
             temp = s; 
             l %= s; 
             s = l;
             l = temp;
        }
        long lcm =  (A / l) * B; // prevents overflow
        System.out.println(l + " " + lcm);
    }
} catch (Exception e) {
    e.printStackTrace();
}

Note: It is not necessary, useful, and in general bad practice to close the scanner when it was created with System.in. This is because that also closes that input stream which will no longer be available for the duration of the program.

WJS
  • 36,363
  • 4
  • 24
  • 39