Merge pull request #1415 from shellhub/dev

Add more math algorithm
This commit is contained in:
Du Yuanchao
2020-08-16 14:50:48 +08:00
committed by GitHub
4 changed files with 147 additions and 22 deletions

View File

@ -115,7 +115,8 @@
* [AbsoluteMin](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteMin.java)
* [AbsoluteValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteValue.java)
* [AmicableNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/AmicableNumber.java)
* [CountDigit](https://github.com/TheAlgorithms/Java/blob/master/Maths/CountDigit.java)
* [Area](https://github.com/TheAlgorithms/Java/blob/master/Maths/Area.java)
* [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java)
* [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java)
* [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java)
* [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java)

119
Maths/Area.java Normal file
View File

@ -0,0 +1,119 @@
package Maths;
/**
* Find the area of various geometric shapes
*/
public class Area {
public static void main(String[] args) {
/* test cube */
assert surfaceAreaCube(1) == 6;
/* test sphere */
assert surfaceAreaSphere(5) == 314.1592653589793;
assert surfaceAreaSphere(1) == 12.566370614359172;
/* test rectangle */
assert surfaceAreaRectangle(10, 20) == 200;
/* test square */
assert surfaceAreaSquare(10) == 100;
/* test triangle */
assert surfaceAreaTriangle(10, 10) == 50;
/* test parallelogram */
assert surfaceAreaParallelogram(10, 20) == 200;
/* test trapezium */
assert surfaceAreaTrapezium(10, 20, 30) == 450;
/* test circle */
assert surfaceAreaCircle(20) == 1256.6370614359173;
}
/**
* Calculate the surface area of a cube.
*
* @param sideLength side length of cube
* @return surface area of given cube
*/
public static double surfaceAreaCube(double sideLength) {
return 6 * sideLength * sideLength;
}
/**
* Calculate the surface area of a sphere.
*
* @param radius radius of sphere
* @return surface area of given sphere
*/
public static double surfaceAreaSphere(double radius) {
return 4 * Math.PI * radius * radius;
}
/**
* Calculate the area of a rectangle
*
* @param length length of rectangle
* @param width width of rectangle
* @return area of given rectangle
*/
public static double surfaceAreaRectangle(double length, double width) {
return length * width;
}
/**
* Calculate the area of a square
*
* @param sideLength side length of square
* @return area of given square
*/
public static double surfaceAreaSquare(double sideLength) {
return sideLength * sideLength;
}
/**
* Calculate the area of a triangle
*
* @param base base of triangle
* @param height height of triangle
* @return area of given triangle
*/
public static double surfaceAreaTriangle(double base, double height) {
return base * height / 2;
}
/**
* Calculate the area of a parallelogram
*
* @param base base of parallelogram
* @param height height of parallelogram
* @return area of given parallelogram
*/
public static double surfaceAreaParallelogram(double base, double height) {
return base * height;
}
/**
* Calculate the area of a trapezium
*
* @param base1 upper base of trapezium
* @param base2 bottom base of trapezium
* @param height height of trapezium
* @return area of given trapezium
*/
public static double surfaceAreaTrapezium(double base1, double base2, double height) {
return (base1 + base2) * height / 2;
}
/**
* Calculate the area of a circle
*
* @param radius radius of circle
* @return area of given circle
*/
public static double surfaceAreaCircle(double radius) {
return Math.PI * radius * radius;
}
}

26
Maths/Average.java Normal file
View File

@ -0,0 +1,26 @@
package Maths;
/**
* Calculate average of a list of numbers
*/
public class Average {
public static void main(String[] args) {
assert average(new double[]{3, 6, 9, 12, 15, 18, 21}) == 12;
assert average(new double[]{5, 10, 15, 20, 25, 30, 35}) == 20;
assert average(new double[]{1, 2, 3, 4, 5, 6, 7, 8}) == 4.5;
}
/**
* Calculate average of a list of numbers
*
* @param numbers array to store numbers
* @return mean of given numbers
*/
public static double average(double[] numbers) {
double sum = 0;
for (double number : numbers) {
sum += number;
}
return sum / numbers.length;
}
}

View File

@ -1,21 +0,0 @@
package Maths;
import java.util.*;
// count the number of digits in a number
public class CountDigit {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int number = sc.nextInt();
int digits = 0;
if(number == 0){
System.out.println("The number of digits present in the number: 1");
}
else
{
digits = (int)Math.floor(Math.log10(Math.abs(number)) + 1);
System.out.println("The number of digits present in the number: " + digits);
}
}
}