Writing a Program for GCD

Asked By 0 points N/A Posted on -
qa-featured

Need help writing a program to find GCD for two input numbers please if you can use this example

GCD (12,15)?

Thanks so much!!

SHARE
Best Answer by lee hung
Answered By 75 points N/A #127064

Writing a Program for GCD

qa-featured

Hi polymoth,

I wish you could have stated in which language you would like to have the program in. By this I mean something like java, visual basic, or maybe c or c++. But I will you one sample program for working out greatest common divisors which is written in java.

Here it is:

import java.io.*;
public class GCD{
public static void main(String[] arguments) throws java.io.IOException {
    
    long gr_cd;
    int number_of_nos = getNumber("Enter the number of numbers to get the gcd from? ");
    long nos_Array[] = new long[number_of_nos];
    
    for(int x=0; x < number_of_nos; x++){
        nos_Array[x] = getNumber("Enter the number " + (x+1) + ": ");
    }    
    
    gr_cd = GCD(nos_Array[0], nos_Array[1]);
    
    if(number_of_nos  > 2){
        for(int y=2; y < number_of_nos ; y++){
            gr_cd = GCD(gr_cd, nos_Array[y]);
        }
    }
    
    System.out.println("The greatest common divisor is " + gr_cd);
               
}
    
static long GCD(long m, long n){
    long x;
    long y;
    while(m%n != 0){
        x = n;
        y = m%n;
        m = x;
        n = y;
    }
    return n;
}            
    
static int getNumber(String question) throws java.io.IOException {
    String theNumber;
    int num = 0;
    BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
    System.out.print(question);
    theNumber = in.readLine();
    System.out.println();
    number = Integer.parseInt(theNumber);
    return num;
}
}

The program is simple; you just customize it to suit your status. The values to change include the number of numbers you need to get the greatest common divisor from, and also the values for declaring variables. Just edit them to suit you.

Hope this helps.

__

Regards,
Lee Hung.

Answered By 0 points N/A #127065

Writing a Program for GCD

qa-featured

want it in c … thanks

Answered By 75 points N/A #127067

Writing a Program for GCD

qa-featured

Thanks for your response polymoth. I have designed for a small program which should help you get the GCD of two numbers in C programming. I have declared the variables to be p and q, but you can customize them to whichever variables you want. The program is as follows:

#include <stdio.h>
#include <conio.h>

int gcd ( int p, int q)
{
while( p!= q) //execute loop until p is equivalent to q
{
if( p > q)
p= p - q; // large - small , variables are stored in the large value
else
q= q - p;
}
return ( p); // p or q is GCD
}

Hope this helps.

__

Regards
Lee Hung

 

Best Answer
Best Answer
Answered By 75 points N/A #127069

Writing a Program for GCD

qa-featured

Polymoth,

Also try this one too: Whichever works best for you then use it. Programming is all about trying different programs and making better the ones that we already have, so all the best and happy coding with C.

#include "stdio.h"

gcd(int p,int q)

{ if(p<=q && q%p == 0)

return p;

        if(q < p)

         return gcd(p,q);

        else

         return gcd(p,q%p);

}

main()

{ int q,p,div;

        printf("Enter 2 numbers");

        scanf("%d%d",&q,&p);

        div=gcd(q,p);

        printf("The GCDof %d and %d is %d",q,p,div);

        return 0;

}

 

Please inform me on the progress.

Thank you.

__

Regards
Lee Hung

 

Answered By 0 points N/A #127070

Writing a Program for GCD

qa-featured

the progress is good am enjoying learning programming

Hey lee would you advice the best language to learn and is there a site i could turn to for self tutoring

Thanks

polymyth!

Answered By 75 points N/A #127071

Writing a Program for GCD

qa-featured

Hello there Polymoth,

When it comes to programming, I would rather not decide for you the language that you might need to focus on, because many people settle for different languages after first of all interacting with quite a number of them. Other people will prefer using java, others C++ or C while others will prefer visual basic to be the easiest to work with. So if I may ask, which language do you think you can really work well with? As in you find it fun to work with, you do not feel like your head is burning up when are using it and it generally just works well for you. Then go for that programming language.

But in my view, the best programming language is java. You can do a lot of things with java, from creating mobile phone applications to very good user interfaces. So if you think you can work well with java, I will advise that you go for it.

You can join many java forums and get to learn a lot from gurus who have using java for a long time, one of the forums being:
http://www.javaprogrammingforums.com/

__

Regards
Lee Hung

Related Questions