Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to program a gcd

+5 votes
asked Jan 18 by HARINI K (210 points)

3 Answers

+2 votes
answered Jan 18 by 20BIT040SUJITH (180 points)
#include <stdio.h>
int main()
    int n1, n2, i, gcd;

    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);

    for(i=1; i <= n1 && i <= n2; ++i)
        // Checks if i is factor of both integers
        if(n1%i==0 && n2%i==0)
            gcd = i;

    printf("G.C.D of %d and %d is %d", n1, n2, gcd);

    return 0;
0 votes
answered Jan 18 by Wren Infante (140 points)
Greatest common divisor can be found with the following:
1. Start with 3 numbers, x,z, and y.
2. Set x and y to the two numbers you need the GCD for.
3. Set z to the largest number between x and y.
4. Decrement z by one, and if x / z == round(x/z) AND y/z == round(y/z) then stop, as you've found your GCD.
0 votes
answered Jan 19 by Peter Minarik (77,930 points)

This is how to use the OnlineGDB

  1. You select "Create New Project" on the left panel.
  2. This will initialize the main middle panel fresh.
  3. On the top right corner select the "Language"
  4. This will create a simple hello world program for the selected language.
  5. You press the green RUN button on the top panel to run your code.
  6. The output (Hello World) will be displayed in the bottom panel.
  7. You're set up and free to modify the code and write your own program in your desired language.
What did you mean by GCD?
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.