commit
ff6b5c7634
25
Maths/Factorial.java
Normal file
25
Maths/Factorial.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
26
Maths/FindMax.java
Normal file
26
Maths/FindMax.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
26
Maths/FindMin.java
Normal file
26
Maths/FindMin.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
24
Maths/MaxValue.java
Normal file
24
Maths/MaxValue.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
24
Maths/MinValue.java
Normal file
24
Maths/MinValue.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
31
Maths/PrimeCheck.java
Normal file
31
Maths/PrimeCheck.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user