answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Something that is similar to an actual program. Example:

Define gcd (a, b) as

a if b==0

b if a==0

gcd (a, b modulo a) if a<=b

gcd (b, a modulo b) if a>b

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is pseudo code for GCD of two numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Define flowchart and draw flowchart for GCD of two numbers?

pictorial representation of a program is called a flowchart


What is the pseudo code for a program that finds the product of two numbers?

int x; //first number int y; //second number int z = x*y;


Can two arrays exist in one pseudocode?

Or even three. Actually, pseudo-code has no rules.


How do you calculate LCM of three numbers by pesudo code?

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.


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