Hi guys! i am stuck in my fraction program. i hope you guys can solve it. here i have posted my code. i am trying to get for e.g. 4/2 need to get me answer like 2/1 or 2/4 show me 1/2. i hope you can help me out. i will really appreciated. thank you

void fraction(int n, int d){
int i,j;
for(i=1;i<=d;i++)
if(n%i==0)
n = n/d;
for(j=1;j<=d;j++)
if(d%j==0)
d = d/n;
cout << n << "/" << d;
}

Please press the code tags button before pasting code. So you would code simplification? The first thing you should probably do is to test whether the fraction is prime. If the test returns false, then you should check what numbers can divide into it. Use while/for loops to test. Obviously, if either the top or bottom number is one, then you won't be able to divide, so you don't need to do all the tests. Again, you should use loops to keep dividing until you get to a prime.

To reduce the fraction you could first find the greatest common denominator (factor) for the numerator and denominator. Then divide both numerator and denominator by the greatest common denominator.

To find the greatest common denominator you can search this board or the internet for commonly used protocols. One approach has been implied by SgtMe, but there are others, too.

public class StudentListings{
private String name; // key field
private int ID;
private double GPA;
private int next;
private int size;
private StudentListing[] data;
public StudentListings(){
this.name= name;
this.id= ID;
this.gpa=GPA;
}
public StudentListings(){
next=0;
data= new StudentListings[Size];
size= Size;
} // end of constructor
...