# Thread: Need some help to symply a fraction

1. ## Need some help to symply a fraction

Hello to everyone as I am new and this is my first post.
My mother lenguage isn´t english but I´ll try my best to explain my self .
I am doing this program in which there is a function which is supposed to symplify a fraction such as 22/24 into 11/12.
The program doesn´t give any compilation errors but it only executes well when you introduce fractions like 24/36 and turns them into 2/3 because the highest common factor of both is found at the end of their vectors (va[c] and vb[c1]).
Code:
```#define d 15#include <stdio.h>

int function1(int a, int b){
int va[d]={0}, vb[d]={0}; int i=0,j=0,c=0,c1=0,r,y;
for(i=1;i<d;++i){if(a%i==0) {va[c]=i;++c;}
if(b%i==0) {vb[c1]=i;++c1;}}
printf("\nThe factor vectors are:\n");
for(i=0;i<d;++i)printf(" %d",va[i]); putchar('\n');
for(i=0;i<d;++i)printf(" %d",vb[i]);
printf("\nValue in c: %d and in c1: %d",c, c1);

/*This is where the problem must be*/
i=c-1; j=c1-1;
while(i!=0){while(j!=0){
if(va[i]==vb[j]) return va[i];
else printf("\n\nva[%d]=%d   !=   vb[%d]=%d",i,va[i],j,vb[j]);
j=j-1;
}
i=i-1;}

/*It should never return '98'. it should have returned '1'
before becouse '1' is a factor of every number */
return 98;
}

int main(){int num, den,r;
scanf("%d", &num);
scanf("%d", &den);
r=function1(num, den);
printf("\nYour symplified fraction is: %d/%d", num/r, den/r);
printf("\nResult picked up from function1 is: %d", r);
return 0;}```
If this doubt I have has been discussed before please just send me to that thread.

2. It might help for you to search the Web for "Euclid's algorithm".

3. That was a great idea after reeding about that magical-mathematical trick Euclid's algorithm.
I changed my function for this one:
Code:

Code:
```int function1(int a, int b){
while(a!=b)
{
if(a>b) a=a-b;
if(b>a) b=b-a;
}
return a;
}```

thank you laserlight for your help!

4. If I'm not mistaken, all that code does is find the greatest common factor (so you have to do the division after calling it).