Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Maths/Factorial.java
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions Maths/FindMax.java
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions Maths/FindMin.java
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions Maths/MaxValue.java
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions Maths/MinValue.java
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions Maths/PrimeCheck.java
Original file line number Diff line number Diff line change
@@ -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;
}
}