/*
Assuming that by "a shell program", you mean "a program that is run from a shell", and not "a shell script", then this would fit the bill.
Note that it could be optimized in many ways, such as:
- checking only for prime factors
- only counting as high as the square root of the smaller number
*/
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ){
int val1, val2, n, gcd = 0;
if( argc != 3 ){
fprintf( stderr, "Insufficent arguments. Syntax:\n\tgcf #a #b\n" );
fflush( stderr );
exit( 1 );
}
val1 = atoi( argv[1] );
val2 = atoi( argv[2] );
if( val1 < 2 val2 < 2 ){
printf( "No common factors\n" );
exit( 0 );
}
for( n = 2; n < val1 && n < val2; n++ ) {
if( val1 % n 0 ){
printf( "No common factors\n" );
}else{
printf( "Greatest common factor is %i\n", gcd );
}
return 0;
}
Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.
For this you will need a couple of helper algorithms. The first is the GCD (greatest common divisor) which is expressed as follows:procedure GCD (a, b) isinput: natural numbers a and bwhile ab doif a>blet a be a-belselet b be b-aend ifend whilereturn aThe second algorithm is the LCM (least common multiple) of two numbers:procedure LCM (a, b) isinput: natural numbers a and b return (a*b) / GCD (a, b)Now that you can calculate the GCD and LCM of any two natural numbers, you can calculate the LCM of any three natural numbers as follows:procedure LCM3 (a, b, c) isinput: natural numbers a, b and c return LCM (LCM (a, b), c)Note that the LCM of three numbers first calculates the LCM of two of those numbers (a and b) and then calculates the LCM of that result along with the third number (c). That is, if the three numbers were 8, 9 and 21, the LCM of 8 and 9 is 72 and the LCM of 72 and 21 is 504. Thus the LCM of 8, 9 and 21 is 504.
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; }
public int findSum(int n1, int n2) { return n1 + n2; }
#include<stdio.h> main() { int num1; int num2; int a; printf("Enter any two numbers :"); scanf("%d%d",&num1,&num2); for(a=num1;num1<=num2;a++) { if ( a % 2 == 1); { printf("%d",a); } } getch(); }
You need at least two numbers to find an LCM.Two or more numbers are needed to find the Lcm
Two or more numbers are needed to find their LCM
Two or more numbers are needed to find their LCM
Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.
No. You can only find the LCM of at least two numbers, prime or otherwise. The LCM of any two prime numbers is their product.
You need at least two numbers to find an LCM.
Two or more numbers are needed to find the LCM
You need at least two numbers to find an LCM.
You need at least two numbers to find an LCM.
The product of the GCF and the LCM of two numbers is equal to the product of the original two numbers. Multiply the GCF and the LCM. The original two numbers will be another factor pair of that total. Find the factor pair that has that GCF and LCM.
You need at least two numbers to find an LCM.
Two or more numbers are normally needed to find the LCM