Skip to content

Commit f6f5dac

Browse files
add more
1 parent 35e85bc commit f6f5dac

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

Maths/Factorial.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Maths;
2+
3+
public class Factorial {
4+
public static void main(String[] args) {
5+
int n = 5;
6+
System.out.println(n + "! = " + factorial(n));
7+
}
8+
9+
/**
10+
* Calculate factorial
11+
*
12+
* @param n the number
13+
* @return the factorial of {@code n}
14+
*/
15+
public static long factorial(int n) {
16+
if (n < 0) {
17+
throw new ArithmeticException("n < 0");
18+
}
19+
long fac = 1;
20+
for (int i = 1; i <= n; ++i) {
21+
fac *= i;
22+
}
23+
return fac;
24+
}
25+
}

Maths/FindMax.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Maths;
2+
3+
public class FindMax {
4+
5+
//Driver
6+
public static void main(String[] args) {
7+
int[] array = {2, 4, 9, 7, 19, 94, 5};
8+
System.out.println("max = " + findMax(array));
9+
}
10+
11+
/**
12+
* find max of array
13+
*
14+
* @param array the array contains element
15+
* @return max value
16+
*/
17+
public static int findMax(int[] array) {
18+
int max = array[0];
19+
for (int i = 1; i < array.length; ++i) {
20+
if (array[i] > max) {
21+
max = array[i];
22+
}
23+
}
24+
return max;
25+
}
26+
}

Maths/FindMin.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Maths;
2+
3+
public class FindMin {
4+
5+
//Driver
6+
public static void main(String[] args) {
7+
int[] array = {2, 4, 9, 7, 19, 94, 5};
8+
System.out.println("min = " + findMax(array));
9+
}
10+
11+
/**
12+
* find min of array
13+
*
14+
* @param array the array contains element
15+
* @return min value
16+
*/
17+
public static int findMax(int[] array) {
18+
int min = array[0];
19+
for (int i = 1; i < array.length; ++i) {
20+
if (array[i] < min) {
21+
min = array[i];
22+
}
23+
}
24+
return min;
25+
}
26+
}

Maths/MaxValue.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Maths;
2+
3+
public class MaxValue {
4+
5+
/**
6+
* Returns the greater of two {@code int} values. That is, the
7+
* result is the argument closer to the value of
8+
* {@link Integer#MAX_VALUE}. If the arguments have the same value,
9+
* the result is that same value.
10+
*
11+
* @param a an argument.
12+
* @param b another argument.
13+
* @return the larger of {@code a} and {@code b}.
14+
*/
15+
public static int max(int a, int b) {
16+
return a >= b ? a : b;
17+
}
18+
19+
public static void main(String[] args) {
20+
int a = 3;
21+
int b = 4;
22+
System.out.format("max:%d between %d and %d", max(a, b), a, b);
23+
}
24+
}

Maths/MinValue.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Maths;
2+
3+
public class MinValue {
4+
5+
/**
6+
* Returns the smaller of two {@code int} values. That is,
7+
* the result the argument closer to the value of
8+
* {@link Integer#MIN_VALUE}. If the arguments have the same
9+
* value, the result is that same value.
10+
*
11+
* @param a an argument.
12+
* @param b another argument.
13+
* @return the smaller of {@code a} and {@code b}.
14+
*/
15+
public static int min(int a, int b) {
16+
return a <= b ? a : b;
17+
}
18+
19+
public static void main(String[] args) {
20+
int a = 3;
21+
int b = 4;
22+
System.out.format("min:%d between %d and %d", min(a, b), a, b);
23+
}
24+
}

Maths/PrimeCheck.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Maths;
2+
3+
import java.util.Scanner;
4+
5+
public class PrimeCheck {
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
9+
System.out.println("Enter n:");
10+
int n = scanner.nextInt();
11+
if (isPrime(n)) {
12+
System.out.println(n + "is prime number");
13+
} else {
14+
System.out.println(n + "is not prime number");
15+
}
16+
}
17+
18+
/***
19+
* Check a number is prime or not
20+
* @param n the number
21+
* @return {@code true} if {@code n} is prime
22+
*/
23+
public static boolean isPrime(int n) {
24+
for (int i = 3; i <= Math.sqrt(n); i += 2) {
25+
if (n % i == 0) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
}

0 commit comments

Comments
 (0)