greatest common divisor (gcd) using C Language
the Greatest Common Divisor (GCD) of two whole numbers, also called the
Greatest Common Factor (GCF) and the Highest Common Factor (HCF), is the
largest whole number that's a divisor (factor) of both of them. For
instance, the largest number that divides into both 20 and 16 is 4.
(Both 16 and 20 have larger factors, but no larger common factors
-- for instance, 8 is a factor of 16, but it's not a factor of 20.) In
grade school, most people are taught a "guess-and-check" method of
finding the GCD. Instead, there is a simple and systematic way of doing
this that always leads to the correct answer. The method is called
"Euclid's algorithm." If you want to know how to truly find the Greatest
Common Divisor of two integers.
for more Details: click here
program:
# include <stdio.h>
/* Definition of the Greatest Comman Divisor Function */
int GCD(int n, int m)
{
if ((n>=m) && ((n%m)==0))
return(m);
else
return GCD(m,(n%m));
}
/* main function */
void main()
{
int no1, no2;
int GCDresult;
clrscr();
printf("Program for GCD caluclation\n\n");
printf("Input 2 integer numbers: ");
scanf("%d%d", &no1,&no2);
printf("\n\n");
GCDresult = GCD(no1, no2);
printf("no1 = %d\n", no1);
printf("no2 = %d\n\n", no2);
printf("Greatest Comman Diviser = %d\n", GCDresult);
getch();
}
/* Definition of the Greatest Comman Divisor Function */
int GCD(int n, int m)
{
if ((n>=m) && ((n%m)==0))
return(m);
else
return GCD(m,(n%m));
}
/* main function */
void main()
{
int no1, no2;
int GCDresult;
clrscr();
printf("Program for GCD caluclation\n\n");
printf("Input 2 integer numbers: ");
scanf("%d%d", &no1,&no2);
printf("\n\n");
GCDresult = GCD(no1, no2);
printf("no1 = %d\n", no1);
printf("no2 = %d\n\n", no2);
printf("Greatest Comman Diviser = %d\n", GCDresult);
getch();
}
No comments:
Post a Comment