package gcd;


import java.io.*;
class gcd 


    {
    public static void main (String args []) throws IOException

        {

       BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

         String strEntry;
         String str_C;

        String ans; 

    int a[] = new int[10000];
    int n=0,i=0,j=0,c=0,max=0,min=0;
    long prod=0;
  strEntry = dataIn.readLine();
  n = Integer.parseInt(strEntry);

  for(i=1;i<=n;i++)
  {


str_C = dataIn.readLine();
   c = Integer.parseInt(str_C);
    if(c>0)
      a[i]=c;
    else
    {
      System.out.print("Invalid Entry");
      return;
    }
  }

 max=a[1];
  for(i=1;i<=n;i++)
    if(a[i]>=max)
      max=a[i];
  min=a[1];
  for(i=1;i<=n;i++)
    if(a[i]<min)
      min=a[i];

  for(i=1,prod=1;i<=n;i++)
    prod=prod*a[i];

System.out.println();

  for(i=max;i<=prod;i+=max)
  {

    c=0;
    for(j=1;j<=n;j++)
      if(i%a[j]==0)
         c+=1;
    if(c==n)
    {

    }
  }

  for(i=min;i>0;i--)
  {
    if (min%i==0)
    {
      c=0;
      for(j=1;j<=n;j++)
         if(a[j]%i==0)
           c+=1;
    }
    if(c==n)
    {
      System.out.print(i);
      break;
    }
  }
}

} 

input
100
2 2 2 2 2 ......2
output
1
Why did not output 2
Please help me

Dani AI

Generated

— two concrete bugs to check first (and is right: reformatting the code makes both obvious).

First, multiplying all inputs into prod is fragile and unnecessary. If you have many numbers (or large ones) the long will overflow and wrap to 0 or a negative value; loops that use prod then behave unpredictably. Second, the final divisor test resets c only inside the if (min % i == 0) block but tests if (c == n) outside of it. That lets a stale c leak between iterations and produce a wrong result. Also watch input parsing: calling readLine() repeatedly expects one number per line; use a token-based reader if numbers may be space-separated.

Fix strategy:

  • Drop the product/LCM brute-force approach.
  • Use the Euclidean algorithm pairwise: start g = a0, then g = gcd(g, a1), etc. It is O(log M) and avoids overflow.
  • Read tokens with Scanner or String.split so whitespace-separated input is handled.
  • Use 0-based arrays (or no array at all) and validate inputs.
  • If numbers can exceed 32-bit, use BigInteger.gcd.

Example (robust, concise) implementation to compute GCD of N positive ints:

import java.util.Scanner;

public class GcdArray {
  static int gcd(int a, int b) {
    a = Math.abs(a);
    b = Math.abs(b);
    while (b != 0) {
      int t = a % b;
      a = b;
      b = t;
    }
    return a;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    if (!sc.hasNextInt()) return;
    int n = sc.nextInt();
    if (n <= 0) return;
    int g = 0;
    for (int i = 0; i < n && sc.hasNextInt(); i++) {
      int v = sc.nextInt();
      if (v <= 0) { System.out.println("Invalid Entry"); return; }
      g = (i == 0) ? v : gcd(g, v);
      if (g == 1) break;
    }
    System.out.println(g);
  }
}

Quick checklist: fix the misplaced if/braces, stop computing the product, read tokens correctly, and use Euclid. These changes remove overflow risks and yield the correct GCD efficiently.

No way can I understand that code without proper indentation and code=java tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.