JAVA-626: Added 3 articles
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.algorithms.gcd;
|
||||
|
||||
public class GCDImplementation {
|
||||
|
||||
public static int gcdByBruteForce(int n1, int n2) {
|
||||
int gcd = 1;
|
||||
for (int i = 1; i <= n1 && i <= n2; i++) {
|
||||
if (n1 % i == 0 && n2 % i == 0) {
|
||||
gcd = i;
|
||||
}
|
||||
}
|
||||
return gcd;
|
||||
}
|
||||
|
||||
public static int gcdByEuclidsAlgorithm(int n1, int n2) {
|
||||
if (n2 == 0) {
|
||||
return n1;
|
||||
}
|
||||
return gcdByEuclidsAlgorithm(n2, n1 % n2);
|
||||
}
|
||||
|
||||
public static int gcdBySteinsAlgorithm(int n1, int n2) {
|
||||
if (n1 == 0) {
|
||||
return n2;
|
||||
}
|
||||
|
||||
if (n2 == 0) {
|
||||
return n1;
|
||||
}
|
||||
|
||||
int n;
|
||||
for (n = 0; ((n1 | n2) & 1) == 0; n++) {
|
||||
n1 >>= 1;
|
||||
n2 >>= 1;
|
||||
}
|
||||
|
||||
while ((n1 & 1) == 0) {
|
||||
n1 >>= 1;
|
||||
}
|
||||
|
||||
do {
|
||||
while ((n2 & 1) == 0) {
|
||||
n2 >>= 1;
|
||||
}
|
||||
|
||||
if (n1 > n2) {
|
||||
int temp = n1;
|
||||
n1 = n2;
|
||||
n2 = temp;
|
||||
}
|
||||
n2 = (n2 - n1);
|
||||
} while (n2 != 0);
|
||||
return n1 << n;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PercentageCalculator {
|
||||
|
||||
public double calculatePercentage(double obtained,double total){
|
||||
return obtained*100/total;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PercentageCalculator pc = new PercentageCalculator();
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.println("Enter obtained marks:");
|
||||
double obtained = in.nextDouble();
|
||||
System.out.println("Enter total marks:");
|
||||
double total =in.nextDouble();
|
||||
System.out.println("Percentage obtained :"+pc.calculatePercentage(obtained,total));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user