Sure, here are 30 Java programs that involve various mathematical calculations, along with their solutions and outputs:
1. **Calculate Factorial:**
```java
import java.math.BigInteger;
public class Factorial {
public static void main(String[] args) {
int n = 5;
BigInteger factorial = BigInteger.ONE;
for (int i = 1; i <= n; i++) {
factorial = factorial.multiply(BigInteger.valueOf(i));
}
System.out.println("Factorial of " + n + " is: " + factorial);
}
}
// Output: Factorial of 5 is: 120
```
2. **Fibonacci Series:**
```java
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;
System.out.print("Fibonacci Series up to " + n + ": ");
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}
// Output: Fibonacci Series up to 10: 0 1 1 2 3 5 8 13 21 34
```
3. **Prime Number Check:**
```java
public class PrimeNumber {
public static void main(String[] args) {
int num = 17;
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
// Output: 17 is a prime number.
```
4. **Greatest Common Divisor (GCD):**
```java
public class GCD {
public static void main(String[] args) {
int num1 = 24, num2 = 36;
int gcd = 1;
for (int i = 1; i <= num1 && i <= num2; i++) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
}
}
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
}
}
// Output: GCD of 24 and 36 is: 12
```
5. **Least Common Multiple (LCM):**
```java
public class LCM {
public static void main(String[] args) {
int num1 = 12, num2 = 18;
int lcm;
// Calculate LCM using the formula: LCM = (num1 * num2) / GCD(num1, num2)
lcm = (num1 * num2) / gcd(num1, num2);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
}
// Method to calculate GCD
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
}
// Output: LCM of 12 and 18 is: 36
```
6. **Armstrong Number Check:**
```java
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153;
int originalNum, remainder, result = 0, n = 0;
originalNum = num;
// Find number of digits
for (; originalNum != 0; originalNum /= 10, ++n);
originalNum = num;
// Calculate Armstrong number
for (; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
result += Math.pow(remainder, n);
}
if (result == num) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
}
// Output: 153 is an Armstrong number.
```
7. **Perfect Number Check:**
```java
public class PerfectNumber {
public static void main(String[] args) {
int num = 28;
int sum = 0;
// Find sum of divisors
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
// Check if number is perfect
if (sum == num) {
System.out.println(num + " is a perfect number.");
} else {
System.out.println(num + " is not a perfect number.");
}
}
}
// Output: 28 is a perfect number.
```
8. **Sum of Digits:**
```java
public class SumOfDigits {
public static void main(String[] args) {
int num = 12345;
int sum = 0;
// Calculate sum of digits
while (num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum);
}
}
// Output: Sum of digits: 15
```
9. **Reverse a Number:**
```java
public class ReverseNumber {
public static void main(String[] args) {
int num = 12345;
int reversed = 0;
// Reverse the number
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed number: " + reversed);
}
}
// Output: Reversed number: 54321
```
10. **Palindrome Check:**
```java
public class Palindrome {
public static void main(String[] args) {
int num = 12321;
int reversed = 0, originalNum;
originalNum = num;
// Reverse the number
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
// Check if originalNum and reversed are equal
if (originalNum == reversed) {
System.out.println(originalNum + " is a palindrome.");
} else {
System.out.println(originalNum + " is not a palindrome.");
}
}
}
// Output: 12321 is a palindrome.
```
11. **Power of a Number:**
```java
public class Power {
public static void main(String[] args) {
int base = 3, exponent = 4;
long result = 1;
// Calculate power using for loop
for (int i = 1; i <= exponent; i++) {
result *= base;
}
System.out.println(base + " raised to the power of " + exponent + " is: " + result);
}
}
// Output: 3 raised to the power of 4 is: 81
```
12. **Square Root Calculation:**
```java
public class SquareRoot {
public static void main(String[] args) {
double num = 25;
double squareRoot = Math.sqrt(num);
System.out.println("Square root of " + num + " is: " + squareRoot);
}
}
// Output: Square root of 25 is: 5.0
```
13. **Binary to Decimal Conversion:**
```java
public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "1010";
int decimal = Integer.parseInt(binary, 2);
System.out.println("Decimal equivalent of " + binary + " is: " + decimal);
}
}
// Output: Decimal equivalent of 1010 is: 10
```
14. **Decimal to Binary Conversion:**
```java
public class DecimalToBinary {
public static void main(String[] args) {
int decimal = 10;
String binary = Integer.toBinaryString(decimal);
System.out.println("Binary equivalent of " + decimal + " is: " + binary);
}
}
// Output: Binary equivalent of 10 is: 1010
```
15. **Factorization:**
```java
public class PrimeFactors {
public static void main(String[] args) {
int num = 60;
System.out.print("Prime factors of " + num + " are: ");
// Print prime factors
for (int i = 2; i <= num; i++) {
while (num % i == 0) {
System.out.print(i + " ");
num /= i;
}
}
}
}
// Output: Prime factors of 60 are: 2 2 3 5
```
16. **Sum of First N Natural Numbers:**
```java
public class SumOfN {
public static void main(String[] args) {
int n = 10;
int sum = n * (n + 1) / 2;
System.out.println("Sum of first " + n + " natural numbers is: " + sum);
}
}
// Output: Sum of first 10 natural numbers is: 55
```
17. **Sum of Squares of First N Natural Numbers:**
```java
public class SumOfSquares {
public static void main(String[] args) {
int n = 5;
int sum = 0;
// Calculate sum of squares
for (int i = 1; i <= n; i++) {
sum += i * i;
}
System.out.println("Sum of squares of first " + n + " natural numbers is: " + sum);
}
}
// Output: Sum of squares of first 5 natural numbers is: 55
```
18. **Sum of Cubes of First N Natural Numbers:**
```java
public class SumOfCubes {
public static void main(String[] args) {
int n = 4;
int sum = 0;
// Calculate sum of cubes
for (int i = 1; i <= n; i++) {
sum += i * i * i;
}
System.out.println("Sum of cubes of first " + n + " natural numbers is: " + sum);
}
}
// Output: Sum of cubes of first 4 natural numbers is: 100
```
19. **HCF of Array of Numbers:**
```java
public class HCFArray {
public static void main(String[] args) {
int[] numbers = {24, 36, 48};
int hcf = numbers[0];
// Calculate HCF of array elements
for (int i = 1; i < numbers.length; i++) {
hcf = gcd(hcf, numbers[i]);
}
System.out.println("HCF of numbers in the array is: " + hcf);
}
// Method to calculate GCD
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
}
// Output: HCF of numbers in the array is: 12
```
20. **LCM of Array of Numbers:**
```java
public class LCMArray {
public static void main(String[] args) {
int[] numbers = {12, 18, 24};
int lcm = numbers[0];
// Calculate LCM of array elements
for (int i = 1; i < numbers.length; i++) {
lcm = lcm(lcm, numbers[i]);
}
System.out.println("LCM of numbers in the array is: " + lcm);
}
// Method to calculate LCM
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
// Method to calculate GCD
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
}
// Output: LCM of numbers in the array is: 72
```
21. **Matrix Addition:**
```java
public class MatrixAddition {
public static void main(String[] args) {
int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}};
int[][] matrix2 = {{7, 8, 9}, {10, 11, 12}};
int rows = matrix1.length;
int columns = matrix1[0].length;
int[][] sum = new int[rows][columns];
// Perform matrix addition
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Display the result
System.out.println("Matrix Addition Result:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
/*
Output:
Matrix Addition Result:
8 10 12
14 16 18
*/
```
22. **Matrix Subtraction:**
```java
public class MatrixSubtraction {
public static void main(String[] args) {
int[][] matrix1 = {{7, 8, 9}, {10, 11, 12}};
int[][] matrix2 = {{1, 2, 3}, {4, 5, 6}};
int rows = matrix1.length;
int columns = matrix1[0].length;
int[][] difference = new int[rows][columns];
// Perform matrix subtraction
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
difference[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
// Display the result
System.out.println("Matrix Subtraction Result:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(difference[i][j] + " ");
}
System.out.println();
}
}
}
/*
Output:
Matrix Subtraction Result:
6 6 6
6 6 6
*/
```
23. **Matrix Multiplication:**
```java
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}};
int[][] matrix2 = {{7, 8}, {9, 10}, {11, 12}};
int rows1 = matrix1.length;
int columns1 = matrix1[0].length;
int columns2 = matrix2[0].length;
int[][] product = new int[rows1][columns2];
// Perform matrix multiplication
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
for (int k = 0; k < columns1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Display the result
System.out.println("Matrix Multiplication Result:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}
/*
Output:
Matrix Multiplication Result:
58 64
139 154
*/
```
24. **Calculate Determinant of a Matrix:**
```java
public class Determinant {
public static void main(String[] args) {
int[][] matrix = {{1, 2}, {3, 4}};
int determinant;
// Calculate determinant of 2x2 matrix
determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
System.out.println("Determinant of the matrix is: " + determinant);
}
}
// Output: Determinant of the matrix is: -2
```
25. **Transpose of a Matrix:**
```java
public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
int rows = matrix.length;
int columns = matrix[0].length;
int[][] transpose = new int[columns][rows];
// Calculate transpose of matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Display the result
System.out.println("Matrix Transpose:");
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
}
/*
Output:
Matrix Transpose:
1 4
2 5
3 6