Program to find GCD.

Here's a program i wrote to find the greatest common divisor of two numbers. I was required by the teacher to use for loops.
The output screen displayed "Enter two numbers:" and I entered 45 and 15 expecting an answer of 15 but I got "gcd is 225". Can someone please tell me what's wrong with the program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 void main()

int A[30],B[30],m,n,i,j,x,z,k,l,gcd=1;

cout<< "enter two numbers";
cin>>m>>n;
for(i=1,j=0;i<=m && j<30;i++)
{
  if(m%i==0)
   { A[j++]=i;
     z=j;
   }
}
for(i=1,j=0;i<=n && j<30;i++)
{ 
  if(n%i==0)
  { B[j++]=i;
    x=j;
  }
}

for(k=z;k>=0;--k)
{
  for(l=x;l>=0;--l)
    {
    if(A[k]==B[l])
      { gcd=gcd*A[k];
      }
    }
}
cout<<"gcd is"<<gcd;
}
Topic archived. No new replies allowed.