#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;
}
pictorial representation of a program is called a flowchart
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.
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.
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; }
for two positive integers: 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; }
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
pictorial representation of a program is called a flowchart
i love u darling
// 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); }
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; }
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.
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
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.
The GCD is 2.
alp for lcm of a no
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 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.