Adding a series of fractions

+2 Patricia Ghann · January 6, 2015
Hello Bucky, Happy New Year. Please I want to write a program in c++ to add a series of fractions and am finding it difficult so I need your help please. I want to add for example 


Post a Reply


- page 1
Oldest  Newest  Rating
0 c student · January 7, 2015
here it is again, this time with simplified fraction result using euclid's gcd algorithm.  i apologize if the algorithm is hard to understand, i had just pulled it out of a piece of code i wrote last year.
#include <stdio.h>
#include <stdlib.h>

// define a struct for a fraction type to hold
// both numerator and denominator
typedef struct {
    int num;
    int den;
} fraction;

fraction fracAdd (fraction, fraction);
fraction simplify (fraction frac);

int main (void) {

    // request amount of fractions
    puts ("Please enter the amount of fractions");
    // acquire number of fractions to be calculated
    int amount = getchar()-48;
    // declare array of fraction types according to the previous line
    fraction frac[amount];
    // declare a resultant fraction
    fraction result;

    // loop to enter in each fraction
    for (int n = 0; n < amount; n++) {
        // request numerator for fraction n
        printf ("Please enter in the numerator for fraction %d\n", n+1);
        // input into fraction array n for numerator member
        scanf ("%d", &frac[n].num);
        // request denominator for fraction n
        printf ("Please enter in the denominator for fraction %d\n", n+1);
        // input into fraction array n for denominator member
        scanf ("%d", &frac[n].den);
        if (frac[n].den == 0) {
            fprintf (stderr, "\nZero is not a valid denominator value\n");
            exit (1);
        // this is just to place the first fraction into result
        if (n == 0) {
            result = frac[n];
        // then for the rest of the fractions, add them onto the result
        } else {
            result = fracAdd (result, frac[n]);

// simplify result with GCD
    result = simplify (result);

    // print result with numerator and denominator
    printf ("Result: %d/%d\n", result.num, result.den);

    return 0;


// function to add fractions
fraction fracAdd (fraction a, fraction b) {
    // declare resultant fraction
    fraction result;
    // adding numerators
    result.num = a.num*b.den+b.num*a.den;
    // adding common denominator
    result.den = a.den*b.den;

    // return resultant
    return result;

// Euclidean algorithm for GCD
fraction simplify (fraction frac) {

    int qot, rmd, m, n;
    int i = 0;
    int j = 0;
    n = frac.num;
    m = frac.den;    

    do {
        if (n >= m) {
            j = 1;
            qot = n/m;
            rmd = n % m;
            if (rmd == 0) {
            if (m == 0) {
            n = m;
            m = rmd;
        } else if (m > n) {
            j = 2;
            qot = m/n;
            rmd = m % n;
            if (rmd == 0) {
            if (n == 0) {
            m = n;
            n = rmd;
    } while (i < 10);

    if (j == 1) {
        frac.num /= m;
        frac.den /= m;
    } else if (j == 2) {
        frac.num /= n;
        frac.den /= n;

    return frac;


note: have not tested for all cases, however should work nicely
0 c student · January 8, 2015
doesn't matter to me.  i only do this so i can learn and if they end up learning too, then that's fine with me.  if they end up failing to provide at a given time, well that's just too bad. :angel:
0 Patricia Ghann · January 8, 2015
Thanks a lot guys for your help.
0 Patricia Ghann · January 6, 2015
This is what I have come out with so far.
using namespace std;

Hello, what seems to be the problem? This program builds and runs alright, but its not doing what I want. I need help have to submit by Thursday. Thanks.

using namespace std;

int main()

{int num = 1;
int den = 3;
float fraction(num/den);
int numberOfFractionEntered = 0;
int sum = 0;
cout<< "Enter first fraction or -1 to quit" <<endl;
cin>> fraction;

while (fraction < 1/3){
sum = sum + fraction;

cout<< "Enter next fraction or -1 to quit" <<endl;

cout<<sum <<endl;

return 0;

0 Number Double07 · January 6, 2015
I think it would be easier if you prompted the user to enter the numerator, then the denominator within a while loop.
0 Patricia Ghann · January 6, 2015
The assignment is : "write a program to sum the following series: 1/3+3/5+5/7+9/11+11/13+13/15 +...............95/97+97/99.

I want  to write the program that allows the user to input the fraction and then sum  all of them up like a sentinel controlled program. Help please today is the last before submission. Thanks
0 Number Double07 · January 6, 2015
Does the answer have to be in a fractional form, or can you have an answer as a float? 
0 Number Double07 · January 6, 2015
This is the way I would approach this.  Initialize all numerator and denominator values at 0.  Then, make the user input numerator and denominator values while in a while loop.  The while loop should be set to run while the numerator is not equal to -1 and or the denominator too, or else it will end.  Then you can just divide the numerator by the denominator, sum it up each time ti runs through the loop.  This should work.  I'm going to try to create it to see if it works.  I'm new to this, so this is a good exercise for me too.
0 Patricia Ghann · January 7, 2015
Please help:(. Don't know what is going on now. I have modified the code and its not doing what its meant to do.

using namespace std;

int main()

int num = 0;
int den = 0;
float fraction;
fraction =float(num)/float(den);
int numberOfFractionEntered = 0;
double sum = 0;
The court<< "Enter first fraction or -1 to quit" <<end;
cin>> fraction;

while (fraction !=-1){
//sum = sum + fraction;
cout<<num<< "/"<< endl;
cin>> den;

cout<< "Enter next fraction or -1 to quit" <<endl;
sum = sum + fraction;

cout<<sum <<endl;

return 0;

0 c student · January 7, 2015
you may want to declare a struct fraction which contains members num and den.  when you request input, you could ask how many fractions they may want to enter in which sets the amount of loops.  you will need to input the numerator and denominator separately for each fraction.

once that's done, you may want to create a loop with a function containing a fraction-adding algorithm. 
> fractionResult.num = fraction1.num*fraction2.den + fraction2.num*fraction1.den
> fractionResult.den = fraction1,den*fraction2.den

an example: if you want to calculate 1/3 + 3/5:
> fractionResult.num = 1*5+3*3 = 5+9 = 14
> fractionResult.den = 3*5 = 15
resulting in a fraction result struct representing 14/15, save the value and add on the next input fraction
  • 1
  • 2



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator