Greatest Common Factors of Fractions

This is a discussion on Greatest Common Factors of Fractions within the C++ Programming forums, part of the General Programming Boards category; I need help finding GCF of this fraction program Code: #include <iostream.h> #include <math.h> int main() { // Declare variables ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Greatest Common Factors of Fractions

    I need help finding GCF of this fraction program
    Code:
    #include <iostream.h>
    #include <math.h>
    
    int main()
    {
    	// Declare variables
    	int numerator;		// Numerator for fraction
    	int denominator;	// Denominator for fraction
    	int a, b, i;		// a = denominator, b = numerator. i = GCF
    	int ii;				// GCF
    	int hold;			// holds output screen
    	
    	// Get fractions from user
    	cout << "Enter the numerator : ";
    	cin  >> numerator;
    	cout << "Enter the denominator : ";
    	cin  >> denominator;
    	
    	// Invalid fractions
    	if (denominator == 0)
    	{
    		cout << "Invalid Operation. Cannot divide fraction by 0." << endl;
    	}
    	
    	if (numerator == 0)
    	{
    		cout << "Invalid Operation. Numerator cannot be zero." << endl;
    	}
    	
    	// Reduce Fractions with For loop
    	a = denominator;
    	b = numerator;
    	for (i = a * b; i > 1; i--)
            {
                    if ((a % i == 0) && (b % i == 0))
                    {
                            a /= i;
                            b /= i;
                    }
            }
        cout << endl;
     	
     	
     	
        // Print results to screen
        cout << "Numerator   = " << b << endl;
        cout << "---------     - " << endl;
        cout << "Denominator = " << a << endl;
        cout << endl;
        //cout << "Greatest Common Factor : " << i << endl;
        cin  >> hold;
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Greatest Common Divisor problem
    By fenixataris182 in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 07:55 PM
  2. Greatest common divisor
    By wiz23 in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2005, 04:50 PM
  3. Greatest common divisor with int and double
    By wiz23 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2005, 04:38 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Greatest Common Factor problem
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 03:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21