answersLogoWhite

0

#define SIZE 100

#include <stdio.h>

int hcf_function(int,int);

int lcm_function(int,int);

int main()

{

int array[SIZE],n,i,choice,LCM,hcf;

printf("Enter No of Elements\n");

scanf("%d",&n);

printf("Enter Elements\n");

for(i=0;i<n;i++)

scanf("%d",&array[i]);

do

{

printf("\n\nEnter Choice\n\n1.HCF\n2.LCM\n3.Exit\n");

scanf("%d",&choice);

switch(choice)

{

case 1: hcf=array[0];

for(i=1;i<n;i++)

hcf=hcf_function(hcf,array[i]);

printf("\nHCF = %d",hcf);

break;

case 2: LCM=array[0];

for(i=1;i<n;i++)

LCM=lcm_function(LCM,array[i]);

printf("\nLCM = %d",LCM);

break;

case 3: break;

default:printf("Wrong Choice");

break;

}

}while(choice!=3);

}

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

Function Name : hcf_function

Purpose : to find hcf

Input : two numbers

Return Value : hcf

Return Type : int

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

int hcf_function(int m,int n)

{

int temp,reminder;

if(m<n)

{

temp=m;

m=n;

n=temp;

}

while(1)

{

reminder=m%n;

if(reminder==0)

return n;

else

m=n;

n=reminder;

}

}

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

Function Name : lcm_function

Purpose : to find LCM

Input : two numbers

Return Value : LCM

Return Type : int

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

int lcm_function(int m,int n)

{

int LCM;

LCM=m*n/hcf_function(m,n);

return LCM;

}

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
JudyJudy
Simplicity is my specialty.
Chat with Judy
More answers

#include <stdio.h>

#include <stdlib.h>

int getGCF(int val1, int val2);

int main(int argc, char *argv[]){

int val1, val2, gcf;

if(argc != 3){

printf("Syntax: gcf val1 val2\n");

return 1;

}

val1 = atoi(argv[1]);

val2 = atoi(argv[2]);

gcf = getGCF(val1, val2);

if(gcf 1; n--){

if(!(val1 % n | val2 % n)){

rval = n;

}

}

return rval;

}

User Avatar

Wiki User

13y ago
User Avatar

The recursive method has no loops so the assumption is to use the iterative method instead. Both methods are shown below, but the iterative method uses a goto rather than the usual while loop (commented out).

#include<iostream>

#include<time.h>

//#define RECURSIVE // uncomment to use recursive method

#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;

again:

//do{

while((b&1)==0)

b>>=1;

if(a>b)

{

unsigned int t=a;

a=b;

b=t;

}

b-=a;

//}while(b);

if(b)

goto again;

return(a<<c);

}

#endif

int main()

{

using std::cout;

using std::endl;

srand((unsigned) time(NULL));

for(unsigned int attempt=0; attempt<10; ++attempt)

{

unsigned int x=rand()%100;

unsigned int y=rand()%100;

unsigned int hcf=gcd(x,y);

cout<<"GCD("<<x<<','<<y<<") = "<<hcf<<endl;

}

cout<<endl;

}

User Avatar

Wiki User

11y ago
User Avatar

#include<iostream>

#include<time.h>

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));

}

// Returns the maximum value in the array

unsigned int max(const unsigned int n[],const unsigned int size)

{

unsigned int max=n[0];

for( unsigned int index=1; index<size; ++index )

if(max<n[index])

max=n[index];

return(max);

}

// Returns the minimum non-zero value in the array

unsigned int min_nz(const unsigned int n[],const unsigned int size)

{

unsigned int min=max(n,size);

for( unsigned int index=0; index<size; ++index )

if(n[index]&&n[index]<min)

min=n[index];

return(min);

}

// Returns the greatest common divisor in the array

unsigned int gcd(const unsigned int n[],const unsigned int size)

{

unsigned int hcf=min_nz(n,size);

for( unsigned int index=0; 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)

{

// generate an array of 2 to 4 elements with values in range 0 to 99.

unsigned int size=rand()%3+2;

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;

}

User Avatar

Wiki User

11y ago
User Avatar

#include<stdio.h>

int main()

{

int a,b,c,d1,d2,d3;

int gcd(int ,int);

printf("Enter three integers: ");

scanf("%d%d%d",&a,&b,&c);

d1=gcd(gcd(a,b),c);

printf("Greatest common divisors is: %d",d1);

return 0;

}

int gcd(int x,int y)

{

int c,a,b;

if(x>=y)

{

a=x;

b=y;

}

else

{

a=y;

b=x;

}

while(1)

{

c = a%b;

if(c==0)

return b;

a = b;

b = c;

}

}

User Avatar

Wiki User

13y ago
User Avatar

$ cat gcd.c

#include

int gcd_recursive(int a, int b)

{

if (b == 0)

return a;

return gcd_recursive(b, a % b);

}

int gcd_nonrecursive(int a, int b)

{

int t;

while (b != 0) {

t = b;

b = a % b;

a = t;

}

return a;

}

int main()

{

printf("GCD of 10 and 25: %d\n", gcd_recursive(10, 25));

printf("GCD of 10 and 25: %d\n", gcd_nonrecursive(10, 25));

return 0;

}

$ gcc gcd.c -o gcd

$ ./gcd

GCD of 10 and 25: 5

GCD of 10 and 25: 5

User Avatar

Wiki User

15y ago
User Avatar

Add your answer:

Earn +20 pts
Q: C program for gcd of two numbers using functions?
Write your answer...
Submit
Still have questions?
magnify glass
imp