answersLogoWhite

0


Best Answer

#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
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 12y ago

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

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

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;

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

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

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

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

}

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 14y ago

$ 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

This answer is:
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
Related questions

Shell program for 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? 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


Define flowchart and draw flowchart for GCD of two numbers?

pictorial representation of a program is called a flowchart


Write a c program to find GCD of two given integers by using both recursive n non recursive functions?

i love u darling


Java program for finding GCD and LCM of two given numbers?

These are the two functions you need: public static int lcm(int i1, int i2) { return (i1*i2/gcd(i1,i2)); } public static int gcd(int i1, int i2) { // using Euclid's algorithm int a=i1, b=i2, temp; while (b!=0) { temp=b; b=a%temp; a=temp; } return a; }


A program to find GCD andLCM of two numbers?

// recursive algorithm to return gcd using Euclid's Algorithm int gcd (int a, int b) { if (a&lt;0) a= -a; if (b&lt;0) b= -b; if (a&lt;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&lt;0) t=-t; return t / gcd (a, b); }


How do you write a C program to find the GCD of two given integers using non recursive functions?

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.


How do you find the numbers given the gcd and lcm?

if the gcd and lcm are given and one of the numbers are also given,multiply the gcd and lcm and divide them by the given number


What is pseudo code for GCD of two numbers?

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.


What is the gcd of any two consecutive even numbers?

The GCD is 2.


What is the assembly language program for GCD of two 16 bit unsigned integers using Intel 8086?

alp for lcm of a no


How do you write a C program to find the GCD and LCM of two numbers using a switch statement?

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


The greatest common factor of 114 and 190?

The GCD is 38. You can find it with a calculator, using prime factorization with exponents, or using one of the many on-line GCD calculators.You can also list the factors of both numbers and find the largest one that they have in common.