This function presumes that both parameters are greater than 0.
int gcd(int m, int n)
{ while( m > 0 )
{
if( n > m )
{ int t = m; m = n; n = t; }
m -= n;
}
return n;
}
Problem Statement for GCD/HCF:
Find GCD/HCF of two digits entered by a user.
Answer:
#include
#include
void main()
{
int x,y,i;
printf("Etner any Two digits = ");
scanf("d",&x,&y);
for(i=x;i>=1;i--)
{
if(x%i==0 && y%i==0)
{
printf("GCD is %d\n",i);
break;
}
}
getch();
}
Use the following function: int gcd (int a, int b) { while (b != 0) { a %= b; a ^= b ^= a ^= b; } return a; } Note that a ^= b ^= a ^= b is an efficient method of swapping two values.
The following function will return the GCD or LCM of two arguments (x and y) depending on the value of the fct argument (GCD or LCM). enum FUNC {GCD, LCM}; int gcd_or_lcm(FUNC fct, int x, int y) { int result = 0; switch (fct) { case (GCD): result = gcd (x, y); break; case (LCM): result = lcm (x, y); break; } return result; }
pictorial representation of a program is called a flowchart
To find the GCD of three numbers, a, b and c, you need to find the GCD of a and b first, such that d = GCD(a, b). Then call GCD(d, c). Although you could simply call GCD(GCD(a, b), c), a more useful method is to use an array and iteratively call the GCD(a, b) function, such that a and b are the first two numbers in the first iteration, which becomes a in the next iteration, while b is the next number. The following program demonstarates this method. Note that the GCD of two numbers can either be calculated recursively or iteratively. This program includes both options, depending on whether RECURSIVE is defined or not. In a working program you'd use one or the other, but the iterative approach is usually faster because it requires just one function call and no additional stack space. The program will create 10 random arrays of integers of length 3 to 5 and process each in turn. Note that the more numbers in the array, the more likely the GCD will be 1. #include<iostream> #include<time.h> #define RECURSIVE // comment out to use iterative method #define ARRAY // comment out to use non-arrays #ifdef RECURSIVE // Returns the GCD of the two given integers (recursive method) unsigned int gcd(unsigned int a, unsigned int b) { if(!a) return(b); if(!b) return(a); if(a==b) return(a); if(~a&1) { if(b&1) return(gcd(a>>1,b)); else return(gcd(a>>1,b>>1)<<1); } if(~b&1) return(gcd(a,b>>1)); if(a>b) return(gcd((a-b)>>1,b)); return(gcd((b-a)>>1,a)); } #else // Returns the GCD of the two given integers (iterative method) unsigned int gcd(unsigned int a, unsigned int b) { if(!a) return(b); if(!b) return(a); int c; for(c=0; ((a|b)&1)==0; ++c) { a>>=1; b>>=1; } while((a&1)==0) a>>=1; do{ while((b&1)==0) b>>=1; if(a>b) { unsigned int t=a; a=b; b=t; } b-=a; }while(b); return(a<<c); } #endif RECURSIVE // Returns the greatest common divisor in the given array unsigned int gcd(const unsigned int n[], const unsigned int size) { if( size==0 ) return( 0 ); if( size==1 ) return( n[0] ); unsigned int hcf=gcd(n[0],n[1]); for( unsigned int index=2; index<size; ++index ) hcf=gcd(hcf,n[index]); return(hcf); } int main() { using std::cout; using std::endl; srand((unsigned) time(NULL)); for(unsigned int attempt=0; attempt<10; ++attempt) { unsigned int size=rand()%3+3; unsigned int* num = new unsigned int[size]; unsigned int index=0; while(index<size) num[index++]=rand()%100; unsigned int hcf=gcd(num,size); cout<<"GCD("; index=0; cout<<num[index]; while(++index<size) cout<<','<<num[index]; cout<<") = "<<hcf<<endl; delete[]num; } cout<<endl; }
public class GCD { public static void main(String[] args) { //Example how to use this method System.out.println(GCD(15,50)); } //find the greatest common divisor of two numbers public static int GCD(int a, int b){ if (b == 0) return a; return GCD(b, a % b); } } Hope this help to solve you problem.
i love u darling
write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? check bellow link http://bashscript.blogspot.com/2009/08/gcd-of-more-than-two-numbers.html
Based on gcd: int LCM (int a, int b) { . int d= gcd (a, b); . if (d==0) return 0; . else return a/d*b; }
A C++ implementation of the Binary GCD (Stern's) algorithm is shown in the Related Link below.
Use the following function: int gcd (int a, int b) { while (b != 0) { a %= b; a ^= b ^= a ^= b; } return a; } Note that a ^= b ^= a ^= b is an efficient method of swapping two values.
The following function will return the GCD or LCM of two arguments (x and y) depending on the value of the fct argument (GCD or LCM). enum FUNC {GCD, LCM}; int gcd_or_lcm(FUNC fct, int x, int y) { int result = 0; switch (fct) { case (GCD): result = gcd (x, y); break; case (LCM): result = lcm (x, y); break; } return result; }
pictorial representation of a program is called a flowchart
find the program in c-pgms.blogspot.com
To find the GCD of three numbers, a, b and c, you need to find the GCD of a and b first, such that d = GCD(a, b). Then call GCD(d, c). Although you could simply call GCD(GCD(a, b), c), a more useful method is to use an array and iteratively call the GCD(a, b) function, such that a and b are the first two numbers in the first iteration, which becomes a in the next iteration, while b is the next number. The following program demonstarates this method. Note that the GCD of two numbers can either be calculated recursively or iteratively. This program includes both options, depending on whether RECURSIVE is defined or not. In a working program you'd use one or the other, but the iterative approach is usually faster because it requires just one function call and no additional stack space. The program will create 10 random arrays of integers of length 3 to 5 and process each in turn. Note that the more numbers in the array, the more likely the GCD will be 1. #include<iostream> #include<time.h> #define RECURSIVE // comment out to use iterative method #define ARRAY // comment out to use non-arrays #ifdef RECURSIVE // Returns the GCD of the two given integers (recursive method) unsigned int gcd(unsigned int a, unsigned int b) { if(!a) return(b); if(!b) return(a); if(a==b) return(a); if(~a&1) { if(b&1) return(gcd(a>>1,b)); else return(gcd(a>>1,b>>1)<<1); } if(~b&1) return(gcd(a,b>>1)); if(a>b) return(gcd((a-b)>>1,b)); return(gcd((b-a)>>1,a)); } #else // Returns the GCD of the two given integers (iterative method) unsigned int gcd(unsigned int a, unsigned int b) { if(!a) return(b); if(!b) return(a); int c; for(c=0; ((a|b)&1)==0; ++c) { a>>=1; b>>=1; } while((a&1)==0) a>>=1; do{ while((b&1)==0) b>>=1; if(a>b) { unsigned int t=a; a=b; b=t; } b-=a; }while(b); return(a<<c); } #endif RECURSIVE // Returns the greatest common divisor in the given array unsigned int gcd(const unsigned int n[], const unsigned int size) { if( size==0 ) return( 0 ); if( size==1 ) return( n[0] ); unsigned int hcf=gcd(n[0],n[1]); for( unsigned int index=2; index<size; ++index ) hcf=gcd(hcf,n[index]); return(hcf); } int main() { using std::cout; using std::endl; srand((unsigned) time(NULL)); for(unsigned int attempt=0; attempt<10; ++attempt) { unsigned int size=rand()%3+3; unsigned int* num = new unsigned int[size]; unsigned int index=0; while(index<size) num[index++]=rand()%100; unsigned int hcf=gcd(num,size); cout<<"GCD("; index=0; cout<<num[index]; while(++index<size) cout<<','<<num[index]; cout<<") = "<<hcf<<endl; delete[]num; } cout<<endl; }
public class GCD { public static void main(String[] args) { //Example how to use this method System.out.println(GCD(15,50)); } //find the greatest common divisor of two numbers public static int GCD(int a, int b){ if (b == 0) return a; return GCD(b, a % b); } } Hope this help to solve you problem.
For first find an example program.
// recursive algorithm to return gcd using Euclid's Algorithm int gcd (int a, int b) { if (a<0) a= -a; if (b<0) b= -b; if (a<b) { int tmp; tmp= a; a= b; b= tmp; } if (b == 0) return a; return gcd (b, a%b); } // LCM using gcd int LCM (int a, int b) { int t; t = a*b; if (t<0) t=-t; return t / gcd (a, b); }