Tuesday 9 October 2012

Function023


Write a program to compute G.C.D. of any number of elements.

#include <stdio.h>
#include <conio.h>
void main ( )
{                  
        int         n, arr[50], gcdn, i;
        clrscr ( );
        printf("\n\n\n\t\t Enter the number of elements..:"); scanf("%d",&n);
       
        for ( i = 0; i < n; i++ )
        {                                      
                    printf("\n\n\n\t\t Enter a number..:"); scanf("%d",&arr[i]);
        }
        gcdn = arr [0];
        for ( i = 1; i < n; i++ )
        {                                        
                    gcdn = gcd ( gcdn, arr[i] );
        }                                                
        printf("\n\n\t\t GCD = %d",gcdn);
        getch ( );
}                                  
                                     
int gcd ( int    n1, int    n2 )
{                                              
        int       d;
        while ( n2 != 0 )
        {                              
                        d = n1 % n2;
                        n1 = n2;
                        n2 = d;
        }
        return n1;
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...