Home Contents





/*      Introduction to program:
        Program - eucrec.c.
        Example of recursive programming.
        Francis O'Donovan 26-1-98. */

/* Files to be included. */
 
#include <stdio.h>

/* Global definitions. */

unsigned int a, b;     /* These will hold the original value
                                of the two numbers. */

/* ************************************************* *

/*      Function: main(). */

main()
{
        /* Declare functions. */

        void Euclid();

        /* Initialize variables. */

        unsigned int x, y;       /* The two numbers whose gcd we want. */
        unsigned int temp;       /* Used to swap x and y if y > x. */

        /* Print introduction. */

        printf( "\n\nProgram: eucrec.c.\n" );
        printf( "Example of recursive programming.\n" );
        printf( "Francis O'Donovan 26-1-98.\n\n" );
 
        /* Input x and y. */

        printf( "This program calculates the GCD of two numbers.\n");
        printf( "Please type in the two numbers.\n" );
        printf( "\n#1: ");
        scanf( "%u", &x );
        printf( "\n#2: ");
        scanf( "%u", &y );

        /* Store original values of x and y. */

        a = x;
        b = y;
 
        /* If y>x swap x and y. */
 
        if( y > x)
        {
                temp = x;
                x = y;
                y = temp;
        }

        /* Call Euclid (x,y). */

        Euclid( x, y );

}

/* ******************************************** */

/*      Function: Euclid().
        Purpose: Calculates the gcd of two numbers.
        Arguements: Two unsigned ints.
        Returns: void.
*/

Euclid( x, y)
unsigned int x, y;
{
        if( y == 0)
                printf( "\nGCD(%u, %u) = %u.\n", a, b, x);
 
        else
                Euclid( y, x%y);
}

/* *********************************************** */


Home Contents



© Francis O'Donovan 1999.