answersLogoWhite

0


Best Answer

As written in the C Programming language to find the LCM for up to 50 numbers. All that needs to be done is compile this. It is my first draft of the program, as it was due for a class 2 hrs ago, but if you wish to try and optimize it, that's your task.

-----------------------------------------------------------------------------------------------------------

#include <stdio.h>

#define NUMFACT 50

void FindFactor(int[]);

int Min(int[]);

int Max(int[]);

int calc(int[]);

int check(int[]);

void bubbleSort(int[]);

void Display(int[],int);

int main()

{

int factors[NUMFACT] = {0}; //FACTORS ARRAY INITIALIZED WITH 0'S

int lcf; //LOWEST COMMON FACTOR

FindFactor(factors);

lcf = calc(factors);

bubbleSort(factors);

Display(factors, lcf);

return(0);

}

void FindFactor(int factors[])

{

int tempvalue; //TEMP VALUE TO BE STORED IN FACTOR ARRAY

int location = 0; //LOCATION IN VECTOR TO PLACE INPUT NUMBER

do

{

printf("Enter factor: ");

scanf("%d",&tempvalue);

if (tempvalue != -1)

{

factors[location] = tempvalue;

}

++location;

} while (tempvalue != -1 && location <=49);

}

int Min(int factors[])

{

int increment; //INCREMENT EACH PASS

int min = Max(factors); //MAX VALUE IN FACTORS CHANGED TO LOWEST

int location; //LOCATION OF THE MIN VALUE IN ARRAY

for(increment = 0 ; increment < 49 ; increment++)

{

if (factors[increment] != 0)

{

if(factors[increment] < min)

{

min = factors[increment];

}

}

}

for(increment = 0 ; increment < 49 ; increment++)

{

if(min 0);

lcf = copyfactors[0];

return(lcf);

}

int check(int factors[])

{

int increment; // INCREMENT EACH PASS

int value = 1; //TRUE VALUE RETURNED IF IF-LOOP IS ALWAYS FALSE

for(increment = 1 ; increment < 49 ; increment++)

{

if (factors[increment] != 0)

{

if (factors[increment-1] != factors[increment])

{

value = 0;

}

}

}

return(value);

}

int Max(int factors[])

{

int max = 0; //START VALUE SINCE # CAN'T BE LESS THAN 0

int increment; //INCREMENT EACH PASS

for(increment = 0 ; increment < 50 ; increment++)

{

if (factors[increment] > max)

{

max = factors[increment];

}

}

return(max);

}

void bubbleSort(int factors[])

{

int numPasses; //LCV THAT CONTROLS # OF PASSES

int lcv; //LOOP CONTROL VARIABLE FOR SORTING

int temp; //HOLDS VALUE DURING SWAP

for(numPasses = 1; numPasses < NUMFACT ; numPasses++)

{

for(lcv = 0 ; lcv < NUMFACT - numPasses ; lcv++)

{

if(factors[lcv] > factors[lcv+1])

{

temp = factors[lcv];

factors[lcv] = factors[lcv+1];

factors[lcv+1] = temp;

}

}

}

}

void Display(int factors[],int lcf)

{

int increment; //INCREMENT EACH PASS

printf("The factors of %d are:",lcf);

for(increment = 0; increment < 50 ; increment++)

{

if(factors[increment] != 0)

{

printf(" %d",factors[increment]);

}

}

printf("\n");

}

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Algorithm for finding GCD and LCM of two given numbers?
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


How do you write a algorithm that gives the GCD of two given numbers?

algorithm GCD (a, b) is:while (a b) doif a > b then a := a - b else b := b - aend whilereturn a


Examples of Euclid's algorithm for finding GCD of large numbers?

Euclid's algorithm is a time-tested method for finding the greatest common divisor (GCD) of two numbers. It's based on the principle that the greatest common divisor of two numbers also divides their difference. This algorithm is efficient and works well for large numbers, making it a practical choice in numerous applications. The algorithm operates in a recursive or iterative manner, continually reducing the problem size until it reaches a base case. Here’s how Euclid's algorithm works: print (gcd (a, b) ) # Output: 3ere &gt;a&gt;b , subtract b from a. Replace a with (a−b). Repeat this process until a and b become equal, at which point, a (or b) is the GCD of the original numbers. A more efficient version of Euclid’s algorithm, known as the Division-based Euclidean Algorithm, operates as follows: Given two numbers a and b, where &gt;a&gt; b, find the remainder of a divided by b, denoted as r. Replace a with b and b with r. Repeat this process until b becomes zero. The non-zero remainder, a, is the GCD of the original numbers. In this example, even though a and b are large numbers, the algorithm quickly computes the GCD. The division-based version of Euclid’s algorithm is more efficient than the subtraction-based version, especially for large numbers, as it reduces the problem size more rapidly. Euclid's algorithm is a fundamental algorithm in number theory, with applications in various fields including cryptography, computer science, and engineering. Its efficiency and simplicity make it a powerful tool for computing the GCD, even for large numbers.


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 Euclid's Algorithm?

Euclid's algorithm is a popular algorithm to compute the GCD of two numbers. Algorithm: Gcd(a,b) = Gcd(b, a mod b), where a&gt;=b and Gcd(a,0) = a Say we want to find the GCD of 72 and 105. 105 mod 72 = 33, so GCD(72,105) = GCD(33,72) 72 mod 33 = 6, so GCD(33,72) = GCD(6,33) 33 mod 6 = 3 so GCD(6,33) = GCD(3,6) 6 mod 3 = 0 so GCD(3,6) = GCD(0,3) = 3. So the GCD of 72 and 105 is 3.


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


What is the LCM of 42 and 7?

The LCM of any two numbers can be found with the following formula:LCM(a,b) = (ab) / GCD (a,b).The GCD of two numbers is best found with the Euclidean algorithm which is as follows:GCD(a,b) =a --if b = 0or GCD(b, a mod b) otherwiseIn the example given we have GCD(42,7) = GCD(7, 0) = 7Then LCM(42,7) = (7*42)/7 = 42Note: mod is the operation of dividing one number by another and taking the remainder. e.g. 7 mod 4 = 3, 12 mod 6 = 0.


What is GCD in C Plus Plus?

A C++ implementation of the Binary GCD (Stern's) algorithm is shown in the Related Link below.


What is the great common denominator?

The GCD is found by finding all the prime numbers that divide into each that multiplied together give the original number.


Find the greatest common divisor in of 11 plus 7i and 18-i?

Use the division algorithm. If b = pa + r, then gcd(b,a) = gcd(a,r). Then you can apply the division algorithm again with a = qr + r' and gcd(a,r) = gcd(r, r'). Note that each time the square norm of the remainder gets smaller and smaller, so eventually this process will terminate and you can get the answer. Here, it should be 1.


What is the gcd of any two consecutive even numbers?

The GCD is 2.