Finding the GCD in C++ Program

Oct 24, 2008 at 12:50am
Can someone write a quick program for me. The purpose of the program is to find the GCD(Greatest Common Denominator) of two integers that are inputed. All common denominators have to be displayed. Then at the end of the output, the greatest will be displayed. It will look something like this:

Numbers inputed:

20, 16

Common Denominators: 1,2,4
Greatest Common Denominator: 4
Oct 24, 2008 at 12:57am
Read the rules... We will not code things for you, and this is 100% likely to be a homework assignment. We aren't just a forum that spits out your homework so you can get a good grade, this is a helping website. At least ATTEMPT to do your own work then we might consider helping you.
Oct 24, 2008 at 1:00am
Well im sorry. Im new to this. Ill write what i can with what ive learned so far. I cant experiment with the program because i dont have the C++ program here at home. It is a school where i have Computer Science as a class.
Last edited on Oct 24, 2008 at 1:01am
Oct 24, 2008 at 1:02am
Here's an idea of how you can do it :). Find the results generated by dividing the numerical input by a loop of values that decrement from the value until half of the numerical input. Make another loop that decrements a single unit from each of these results, if the result of the new loop ever turns up a value of 0, then you have yourself a denominator. Store the reults that return true to being a denomiator for all inputted numbers. Check the new results from both inputs against eachother to find the common denominators, find the highest common denominator.
Oct 24, 2008 at 1:04am
What OS/what program do you use at school/home?
Oct 24, 2008 at 1:06am
Microsoft Vision C++
Oct 24, 2008 at 1:27am
well ive started out basic

#include<iostream.h>
#include<iomanip.h>
#include<math.h>
int main()

{

int x=0;
int y=0;

cout<<"Please enter two integers"<<endl;
cin>>x;
cin>>y;

if ((x<0)||(y<0))
{
cout<<"Cannot use Negative Integers"<<endl;
cout<<"Please enter positive integers"<<endl;
cin>>x;
cin>>y;

for(y=x;y<=x;--y)
if(x%y=0)
cout<<y<<endl;

should it look sumthing like that?

Last edited on Oct 24, 2008 at 1:29am
Oct 24, 2008 at 12:40pm
No.

I suggest you get yourself a free C++ compiler for home use.

I also suggest having a look at:

http://en.wikipedia.org/wiki/Greatest_common_divisor

which gives you the algorithm you need to compute the GCD (you'll need to look up the algorithm for LCM to do this).
Oct 24, 2008 at 2:36pm
http://www.bloodshed.net/

Takes maybe a few minutes ;)
Oct 24, 2008 at 2:51pm
Actually, I'm wrong... you don't need LCM; rather, you need to use Euclid's algorithm to find the GCD directly.

Here's a templated version that can find the GCD of constants known at compile time. You just need to convert this to a function that can take values at run time and do the same calculation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

template< size_t A, size_t B >
struct GCD {
    enum { value = GCD< B, A % B >::value };
};

template< size_t A >
struct GCD<A,0> {
    enum { value = A };
};

int main() {
    std::cout << "GCD( 12, 18 ) = " << GCD<12, 18>::value << std::endl;
}


Now you write:

1
2
3
4
int gcd( int a, int b ) {
  // ...  Hint: on the above Wiki page, click on Euclid's algorithm and it will give
  // you pseudo-code for the if-else you need.
}
Oct 24, 2008 at 3:57pm
well ive started out basic

#include<iostream.h>
#include<iomanip.h>
#include<math.h>
int main()

{

int x=0;
int y=0;

cout<<"Please enter two integers"<<endl;
cin>>x;
cin>>y;

if ((x<0)||(y<0))
{
cout<<"Cannot use Negative Integers"<<endl;
cout<<"Please enter positive integers"<<endl;
cin>>x;
cin>>y;

for(y=x;y<=x;--y)
if(x%y=0)
cout<<y<<endl;

should it look sumthing like that?


1. Please use code tags + indention
2. For checking input a do ... while can be used.
3. Where did you get that algoritmn from? It's not correct. Find a working algoritmn and convert it to C++.

The simplest but not fastest algoritmn is to keep subtracting the lowest number from the highest one until you get zero.
Then return the value of the former lowest number. (The former highest one became zero)
Oct 25, 2008 at 1:54am
here's a hint, I won' t write the code for you:

the GCD of x and y is y if x mod y is 0

otherwise the GCD is the GCD of y and the remainder of x/y

look for recursive functions in your textbook, gcd is one of the simplest ones to write and understand.
Last edited on Oct 25, 2008 at 1:54am
Topic archived. No new replies allowed.