Thread: wnat to know some logic about gcd

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Lightbulb wnat to know some logic about gcd

    i can calculate gcd between 2 and 3 number but how can i calculate gcd multiple nuber i,e 10 number 20 number .
    i need logic and idea. possibly help me for coding.
    here is my effort. i want to modify it for calculate multiple number.


    Code:
    #include<stdio.h>
    int main()
    {
        int j,k,a,b,c,l;
        scanf("%d%d%d",&a,&b,&c);
        l=a;
    
    while(k!=0)
        {
        k=a%b;
        a=b;
        b=k;
    
        }
    
    while(j!=0)
        {
        j=l%c;
        l=c;
        c=j;
    
        }
    
    
        if(l==a)
        printf("\n gcd is %d",a);
        else
        printf("\n gcd is 1");
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Couldn't you just calc the gcd between the first two numbers, and then in a loop calc the gcd of the previous gcd and the next number.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The main things you should change for that are to:
    put the gcd of two numbers into a function, and
    store the input numbers in an array, and
    use a loop over the values in the array to call the gcd function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    In addition to oogabooga's and iMalc's suggestions you should really use better variable names.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the logic?
    By beycan in forum C Programming
    Replies: 3
    Last Post: 10-23-2010, 05:10 AM
  2. Logic help...
    By csharp100 in forum C Programming
    Replies: 8
    Last Post: 09-14-2010, 11:29 PM
  3. Logic?
    By Kurious in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 07:36 PM
  4. help with logic please
    By Tryin in forum C Programming
    Replies: 7
    Last Post: 01-28-2003, 02:54 AM
  5. logic
    By nupe02 in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2003, 10:22 AM

Tags for this Thread