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;
}

Recommended Answers

All 5 Replies

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.

thank you guys. that help me alot i really appreciated. thank you

Is this thread solved? If so, mark solved and upvote the posts that helped. Thanks :)

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.