diff --git a/Maths/Factorial.java b/Maths/Factorial.java new file mode 100644 index 000000000..1734e93fb --- /dev/null +++ b/Maths/Factorial.java @@ -0,0 +1,25 @@ +package Maths; + +public class Factorial { + public static void main(String[] args) { + int n = 5; + System.out.println(n + "! = " + factorial(n)); + } + + /** + * Calculate factorial + * + * @param n the number + * @return the factorial of {@code n} + */ + public static long factorial(int n) { + if (n < 0) { + throw new ArithmeticException("n < 0"); + } + long fac = 1; + for (int i = 1; i <= n; ++i) { + fac *= i; + } + return fac; + } +} diff --git a/Maths/FindMax.java b/Maths/FindMax.java new file mode 100644 index 000000000..739cda39b --- /dev/null +++ b/Maths/FindMax.java @@ -0,0 +1,26 @@ +package Maths; + +public class FindMax { + + //Driver + public static void main(String[] args) { + int[] array = {2, 4, 9, 7, 19, 94, 5}; + System.out.println("max = " + findMax(array)); + } + + /** + * find max of array + * + * @param array the array contains element + * @return max value + */ + public static int findMax(int[] array) { + int max = array[0]; + for (int i = 1; i < array.length; ++i) { + if (array[i] > max) { + max = array[i]; + } + } + return max; + } +} diff --git a/Maths/FindMin.java b/Maths/FindMin.java new file mode 100644 index 000000000..2a13fe4b5 --- /dev/null +++ b/Maths/FindMin.java @@ -0,0 +1,26 @@ +package Maths; + +public class FindMin { + + //Driver + public static void main(String[] args) { + int[] array = {2, 4, 9, 7, 19, 94, 5}; + System.out.println("min = " + findMax(array)); + } + + /** + * find min of array + * + * @param array the array contains element + * @return min value + */ + public static int findMax(int[] array) { + int min = array[0]; + for (int i = 1; i < array.length; ++i) { + if (array[i] < min) { + min = array[i]; + } + } + return min; + } +} diff --git a/Maths/MaxValue.java b/Maths/MaxValue.java new file mode 100644 index 000000000..b1b258685 --- /dev/null +++ b/Maths/MaxValue.java @@ -0,0 +1,24 @@ +package Maths; + +public class MaxValue { + + /** + * Returns the greater of two {@code int} values. That is, the + * result is the argument closer to the value of + * {@link Integer#MAX_VALUE}. If the arguments have the same value, + * the result is that same value. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + */ + public static int max(int a, int b) { + return a >= b ? a : b; + } + + public static void main(String[] args) { + int a = 3; + int b = 4; + System.out.format("max:%d between %d and %d", max(a, b), a, b); + } +} diff --git a/Maths/MinValue.java b/Maths/MinValue.java new file mode 100644 index 000000000..ef1d6a085 --- /dev/null +++ b/Maths/MinValue.java @@ -0,0 +1,24 @@ +package Maths; + +public class MinValue { + + /** + * Returns the smaller of two {@code int} values. That is, + * the result the argument closer to the value of + * {@link Integer#MIN_VALUE}. If the arguments have the same + * value, the result is that same value. + * + * @param a an argument. + * @param b another argument. + * @return the smaller of {@code a} and {@code b}. + */ + public static int min(int a, int b) { + return a <= b ? a : b; + } + + public static void main(String[] args) { + int a = 3; + int b = 4; + System.out.format("min:%d between %d and %d", min(a, b), a, b); + } +} diff --git a/Maths/PrimeCheck.java b/Maths/PrimeCheck.java new file mode 100644 index 000000000..8f32f6b64 --- /dev/null +++ b/Maths/PrimeCheck.java @@ -0,0 +1,31 @@ +package Maths; + +import java.util.Scanner; + +public class PrimeCheck { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter n:"); + int n = scanner.nextInt(); + if (isPrime(n)) { + System.out.println(n + "is prime number"); + } else { + System.out.println(n + "is not prime number"); + } + } + + /*** + * Check a number is prime or not + * @param n the number + * @return {@code true} if {@code n} is prime + */ + public static boolean isPrime(int n) { + for (int i = 3; i <= Math.sqrt(n); i += 2) { + if (n % i == 0) { + return false; + } + } + return true; + } +}