I have a program where it will take any fraction and reduce it if possible, but my only problem with it is prime numbers. If the user inputs something such as 11/7, it will return 2 + 1/7 instead of 2 + 3/7, or if 7/11 is entered, a big mess is just returned. Everything else works fine though. Not sure where to go from here. Here is my code so far.
Code:
#include <stdio.h>
int main (void)
{
int a, b, temp=0,m=0;
int gcd(int,int);
int lcm(int,int);
int whole, minus, top, bottom, top2, bottom2;
printf ("Enter fraction: ");
scanf ("%d/%d",&a,&b);
whole = a / b;
minus = a - (whole*b);
top = lcm(a,b) / b;
bottom = lcm(a,b) / gcd(a,b);
if ((a-b<=0))
{
temp=a;
a=b;
b=temp;
}
if(whole == 0)
printf("result: %d/%d\n", top, bottom);
while(whole != 0){ /
if(a % b == 0)
printf("result: %d\n", whole);
break;
}
if( a % b > 0 && a <= 9) /
printf("result: %d + %d/%d\n", whole, minus, b);
top2 = top / top;
bottom2 = bottom / top;
if( a % b > 0 && a > 9)
printf("result: %d + %d/%d\n", whole, top2, bottom2);
printf("PROGRAM ENDS\n");
return 0;
}
int gcd(a,b)
{
int t,r;
r=a%b;
if (r==0)
return b;
else
return gcd(b,r);
}
int lcm(a,b)
{
int m,n;
m=a*b;
n=m/gcd(a,b);
return n;
}