Code Scrappers


Greatest Common Devisor - C Program


                    #include<stdio.h>
                    #include<stdlib.h>

                    int gcd(int m,int n)
                    {
                        int remainder;

                        while(n!=0)
                        {
                            remainder = m % n;

                            m = n;

                            n = remainder;

                        }

                        return(m);

                    }

                    int main()
                    {
                        int n1, n2, n3, gcd1, gcd2;

                        printf("enter 3 positive integers");

                        scanf("%d%d%d", &n1, &n2, &n3);

                        if(n1 == 0 && n2 == 0 && n3 == 0)
                        {
                            printf("\n Invalid number");

                            exit(0);

                        }

                        gcd1 = gcd(n1, n2);

                        gcd2 = gcd(n3, gcd1);

                        printf("\n GCD is : %d\n", gcd2);

                        return 0;
                    }


                



Output

            Enter three positive integers
            8
            21
            9

            GCD is : 1