Greatest Common Factor problem

This is a discussion on Greatest Common Factor problem within the C++ Programming forums, part of the General Programming Boards category; Im trying to get the greatest common factor for 2 numbers you have the user enter the first number then ...

  1. #1
    Unregistered
    Guest

    Question Greatest Common Factor problem

    Im trying to get the greatest common factor for 2 numbers
    you have the user enter the first number
    then you have the second entered by the user
    this is my basic formula and i haven't been doing this for a while so any help would be greatly appreciated.


    All I really need is a basic formula to get it to work

    #include<iostream.h>
    int main()
    {
    int first,second;
    cout<<"Enter the first number ";
    cin>>first;
    cout<<"Enter the second number ";
    cin>>second;

    if (first==second || second==first)
    cout<<"The Greatest Common Factor is "<<second<<endl;

    if(first>second)
    cout<<"The Greatest Common Factor is " <<

    if(second>first)
    cout<<"The Greatest Common Factor is " <<

    return(0);
    }

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include "stdafx.h"
    #include<iostream.h>
    
    int GetNumber();
    int GetSmallest(int, int);
    int Factor(int,int, int);
    
    int main()
    {
    	int x, y;
    
    	x = GetNumber();
    	y = GetNumber();
    	int smallest = GetSmallest(x,y);
    	
    	int gcf = Factor(x,y,smallest);
    	
    	cout << "The GCF is: " << gcf << endl;
    
    	return 0;
    }
    
    int GetNumber()
    {
    	int n;
    	cout << "Enter a number: ";
    	cin >> n;
    	return n;
    }
    
    int GetSmallest(int x, int y)
    {
    	if (x > y)
    	{ 
    		return y;
    	}
    	return x;
    }
    
    int Factor(int x, int y, int smallest)
    {
    	int gcf = 0;
    
    	if (x > 0 && y > 0)
    	{
    		for(int i = 1; i <= smallest; i++)
    		{
    			if(x % i == y % i)
    				gcf = i;
    		}
    	}
    	return gcf;
    }
    This seems to work. I tested it a few times. Try it.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay I tested it a couple more times and I found an error. I'll fix it in one minute.

    Code:
    int Factor(int x, int y, int smallest)
    {
    	int gcf = 0;
    
    	if (x > 0 && y > 0)
    	{
    		for(int i = 1; i <= smallest; i++)
    		{
    			if((x % i == 0) && (y % i == 0) )
    				gcf = i;
    		}
    	}
    	return gcf;
    }
    This change to the Factor function seems to correct the problem.
    Last edited by Troll_King; 10-08-2001 at 03:33 PM.

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. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 03:33 AM
  5. Greatest Common Factor
    By NavyBlue in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 02:47 PM

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