a recursive function that calculates the greatest common divisor from user's input in java
Java
public int gcd(int a, int b) {
if (b==0) return a;
return gcd(b,a%b);
}/*
import scanner and instantiate scanner class;
declare your method with two parameters
declare a third variable;
set condition;
swap the parameter values if condition is met;
set second conditon based on result of first condition;
divide and assign remainder to the third variable;
swap the result;
in the main method, allow for user input;
Call the method;
*/
public class gcf {
public static void main (String[]args){//start of main method
Scanner input = new Scanner (System.in);//allow for user input
System.out.println("Please enter the first integer: ");//prompt
int a = input.nextInt();//initial user input
System.out.println("Please enter a second interger: ");//prompt
int b = input.nextInt();//second user input
Divide(a,b);//call method
}
public static void Divide(int a, int b) {//start of your method
int temp;
// making a greater than b
if (b > a) {
temp = a;
a = b;
b = temp;
}
while (b !=0) {
// gcd of b and a%b
temp = a%b;
// always make a greater than b
a =b;
b =temp;
}
System.out.println(a);//print to console
}
}/**
* Java program to demonstrate How to find Greatest Common Divisor or GCD of
* two numbers using Euclid’s method. There are other methods as well to
* find GCD of two number in Java but this example of finding GCD of two number
* is most simple.
*
* @author Javin Paul
*/
public class GCDExample {
public static void main(String args[]){
//Enter two number whose GCD needs to be calculated.
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter first number to find GCD");
int number1 = scanner.nextInt();
System.out.println("Please enter second number to find GCD");
int number2 = scanner.nextInt();
System.out.println("GCD of two numbers " + number1 +" and "
+ number2 +" is :" + findGCD(number1,number2));
}
/*
* Java method to find GCD of two number using Euclid's method
* @return GDC of two numbers in Java
*/
private static int findGCD(int number1, int number2) {
//base case
if(number2 == 0){
return number1;
}
return findGCD(number2, number1%number2);
}
}
Output:
Please enter first number to find GCD
54
Please enter second number to find GCD
24
GCD of two numbers 54 and 24 is :6
Also in Java:
- Title
- java permutation
- Category
- Java
- Title
- java get int from double without rounding
- Category
- Java
- Title
- java break in foreach
- Category
- Java
- Title
- string array in java methods
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- jackson ignore values if empty
- Category
- Java
- Title
- intellij replace all
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- spigot get player from UUID
- Category
- Java
- Title
- write a java program to check whether given number is binary or not
- Category
- Java
- Title
- Fab to CircularRevealFrameLayout example
- Category
- Java
- Title
- not equal java
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- blurview android github
- Category
- Java
- Title
- iterate through hashmap java
- Category
- Java
- Title
- loop and save letters in a string java
- Category
- Java
- Title
- difference between compile and execute in java
- Category
- Java
- Title
- java check if int
- Category
- Java
- Title
- java create arraly list
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- java find duplicates in array
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- spark write partitionby
- Category
- Java
- Title
- java uuid
- Category
- Java
- Title
- array in line java
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- how to write a java for loop?
- Category
- Java
- Title
- java scanner
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- how to add java_home in mac
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- Manifest merger failed androidx
- Category
- Java
- Title
- JavaFX onMouseEntered
- Category
- Java
- Title
- how to print the map in java
- Category
- Java
- Title
- java while loop example
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- java 8 group a collections by 2 property
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- center textview programmatically android
- Category
- Java
- Title
- java vs python
- Category
- Java
- Title
- sublist in java
- Category
- Java
- Title
- all data types in java
- Category
- Java
- Title
- java create window
- Category
- Java
- Title
- fibonacci sequence java
- Category
- Java
- Title
- minecraft sounds spigot
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- toString convert to long
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- java.lang.long cannot be cast to java.lang.integer
- Category
- Java
- Title
- Category
- Java
- Title
- java decler variabel
- Category
- Java
- Title
- HOW TO SUPRESS sonar warning in java code
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- java singleton implementation
- Category
- Java
- Title
- java string reverse
- Category
- Java
- Title
- java bigdecimal third root
- Category
- Java
- Title
- pass list to intent in android java
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- foreach not applicable to type - binary tree sort
- Category
- Java
- Title
- como detener un void java
- Category
- Java
- Title
- java int
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- value receive null with post method in the java spring controller
- Category
- Java
- Title
- abstract class in java
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- declare int java
- Category
- Java
- Title
- math.pow java
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- java bukkit double jump
- Category
- Java
- Title
- declareing a variable in java
- Category
- Java
- Title
- float to string java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- throwing exceptions java
- Category
- Java
- Title
- java string length validation regex
- Category
- Java
- Title
- java string to double
- Category
- Java
- Title
- string remove last character
- Category
- Java
- Title
- reentrantreadwritelock java
- Category
- Java
- Title
- ujava saum of positive integers
- Category
- Java
- Title
- enumerated data types in java
- Category
- Java
- Title
- how to return the lower of two values in one line java
- Category
- Java
- Title
- casting java
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- java connect to mysql
- Category
- Java
- Title
- bootstrap alert
- Category
- Java