Results 1 to 6 of 6

Thread: Program to print GCD of given numbers

  1. #1
    Join Date
    Nov 2009
    Posts
    4

    Program to print GCD of given numbers

    Hi All,

    I need you help in writing a program in C or C++ programming language.
    Suppose we have a program while execution of the program code, if we pass the two integer number then the desired output should be the GCD(Greatest Common Divisor) of the given two numbers.

    Please provide the program code to print the GCD of the given number. Even hint also will works for me.

    Your help will be greatly appreciated.
    Last edited by Bhavesh_seth; 27-11-2009 at 08:56 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Program to print GCD of given numbers

    Hi friends,

    I have used the the concept of the "Goto" statement to find out GCD of the two given number.
    I think it will also easy for you to understand the "Goto" statement than complex "For" loop. Please study below program carefully:


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

    int main( )
    {

    int x, y, tm, rm,
    clrscr( );

    printf("Enter the two number:");
    scanf(" %d%d",&x, &y);

    if(x==0)
    { printf("\n GCD of number is : %d", y);
    goto last;
    }
    if(b==0)

    { printf("\n GCD of number is : %d", x);
    goto last;
    }

    /*I took a empty 'for' loop


    for( ; ; )
    {
    rm=x%y;
    if(rm==0) break;
    x= y;
    x= rm;
    }
    printf("\n GCD of Number is : %d", y);

    last:
    getch( );
    return 0;
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Program to print GCD of given numbers

    Hi,

    You should use "While loop" to find out the GCD of the inputted numbers. For reference take the following example which will calculates the GCD of numbers "a1" and "a2". The condition "a1!=a2" from the "While loop" first check whether two variables are identical or not:

    void main()

    {

    int a1,a2;

    clrscr();

    printf("\nEnter two numbers:");

    scanf("%d %d",&a1,&a2);

    while(a1!=a2)

    {

    if(a1>=a2-1)

    a1=a1-a2;

    else

    a2=a2-a1;

    }

    printf("GCD of given numbers =%d",a1);

    getch();

    }
    I hope above program may helpful for you..

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Program to print GCD of given numbers

    Dear friend,

    I get below program from one of the C programming book, and which will explain you the logic of finding GCD of the given number:

    #include <iostream>

    int main()
    {


    int p;
    int Q;
    int t ;
    int r = 1;

    cout << endl ;
    cout << "Enter the first number (positive integer) : " ;
    cin >> p ;
    cout << "Enter the second number (positive integer) : " ;
    cin >> q;


    cout << "Inputted numbers are: " << p << " , " <<q<< endl ;


    if ((p == 0) || (q == 0))
    {
    cout << "ERROR: Value of the 0 has been entered." ;
    return (-1);
    }

    if (p < q)
    {

    t = p;
    p = q;
    q = t;

    }


    while ( r != 0 )

    {

    t = p / q;

    r = x % y;

    p = q;

    p = r;

    }


    cout << endl ;
    cout << "The GCD of given numbers is: " << q << endl ;

    return (0);
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Program to print GCD of given numbers

    It is very easy for me to write the program for GCD of 2 numbers. As I have sound knowledge about the various looping. Take below code and try to run. This code may contains some syntax error as I haven't executed it.

    void main()
    {

    int p1,p2,gcd_demo;
    clrscr();
    printf("Enter numbers: ");

    scanf("%d %d",&p1,&p2);
    gcd_demo=findgcd_demo(p1,p2);
    printf("\nGCD of %d and %d is: %d",p1,p2,gcd);
    getch();
    }
    int findgcd_demo(int l,int m)
    {
    while(l!=m)
    {
    if(l>m)
    return findgcd_demo(l-m,m);
    else
    return findgcd_demo(l,m-l);
    }
    return l;
    }

  6. #6
    Join Date
    Mar 2011
    Posts
    1

    Re: Program to print GCD of given numbers

    #include<stdio.h>
    main()
    {
    int x,y,i;
    printf("\n enter two numbers:");
    scanf("%d%d",&x,&y);
    for(i==x;i>=1;i--)
    {
    if(x%i==0&&y%%i==0)
    {
    printf("\n gcd of two numbers is %d",i);
    break;
    }
    }
    }

Similar Threads

  1. C Program to print Combination of numbers
    By Prashobh Mallu in forum Software Development
    Replies: 6
    Last Post: 26-03-2010, 09:21 PM
  2. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  3. C sharp program to print random numbers
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 12:07 PM
  4. Program to print odd and even numbers
    By roodiii in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 02:13 PM
  5. What is the program to swap two numbers?
    By Bharat89 in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 09:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,928,707.89698 seconds with 17 queries