Program to add two fractions in C
Overflow bug



If you use the input:

1
1/100000 2/100000


You get the result:


Case 1: 0/14100



The problem is with this line:


    den3 = (den1*den2)/den3;



Here, all three variables are 100000.  But den1*den2 overflows an integer.  It would be better to write this as:

    den3 = (den1/den3)*den2;


This expression will work as long as the common denominator fits in an int. 
 However, it will still overflow when the common denominator is larger than what fits in an int.  For this reason, you might want to use long long variables to avoid the problem.

Simplify the result?

If you use the input:

1
1/4 1/4


the answer the program gives is:


Case 1: 2/4



This is correct, but the answer could also be simplified to 1/2.  This is purely a cosmetic suggestion, since the specification didn't say the answer had to be reduced to its simplest form.