surajkumasblog
surajkumasblog
Untitled
69 posts
Don't wanna be here? Send us removal request.
surajkumasblog · 2 months ago
Text
0 notes
surajkumasblog · 2 months ago
Text
0 notes
surajkumasblog · 2 months ago
Text
0 notes
surajkumasblog · 2 months ago
Text
0 notes
surajkumasblog · 2 months ago
Text
Implementing 2x2 Matrix Multiplication in Java: A Step-by-Step Guide
Matrix multiplication is a fundamental operation in linear algebra and is widely used in various applications such as graphics programming, machine learning, and scientific computing. In this post, I will walk you through implementing 2x2 matrix multiplication in Java, explaining the logic and providing a sample code snippet.
Understanding 2x2 Matrix Multiplication Matrix multiplication follows the row-by-column multiplication rule. Given two 2x2 matrices:
𝐴
[ 𝑎 11 𝑎 12 𝑎 21 𝑎 22 ] A=[ a 11 ​
a 21 ​
a 12 ​
a 22 ​
​ ]
𝐵
[ 𝑏 11 𝑏 12 𝑏 21 𝑏 22 ] B=[ b 11 ​
b 21 ​
b 12 ​
b 22 ​
​ ] The resulting matrix
𝐶
𝐴 × 𝐵 C=A×B is calculated as follows:
𝐶
[ ( 𝑎 11 × 𝑏 11 + 𝑎 12 × 𝑏 21 ) ( 𝑎 11 × 𝑏 12 + 𝑎 12 × 𝑏 22 ) ( 𝑎 21 × 𝑏 11 + 𝑎 22 × 𝑏 21 ) ( 𝑎 21 × 𝑏 12 + 𝑎 22 × 𝑏 22 ) ] C=[ (a 11 ​ ×b 11 ​ +a 12 ​ ×b 21 ​ ) (a 21 ​ ×b 11 ​ +a 22 ​ ×b 21 ​ ) ​
(a 11 ​ ×b 12 ​ +a 12 ​ ×b 22 ​ ) (a 21 ​ ×b 12 ​ +a 22 ​ ×b 22 ​ ) ​ ] Java Implementation Below is a Java program that performs 2x2 matrix multiplication:
java Copy Edit public class MatrixMultiplication { public static void main(String[] args) { // Define two 2x2 matrices int[][] A = { {1, 2}, {3, 4} }; int[][] B = { {5, 6}, {7, 8} }; // Call the multiply function int[][] result = multiplyMatrices(A, B); // Display the result System.out.println("Resultant Matrix:"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } } // Function to multiply two 2x2 matrices public static int[][] multiplyMatrices(int[][] A, int[][] B) { int[][] C = new int[2][2]; // Resultant 2x2 matrix // Applying matrix multiplication formula C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0]; C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1]; C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0]; C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1]; return C; }
} Explanation of the Code We define two 2x2 matrices (A and B) and initialize them with values. The multiplyMatrices() function computes the resultant 2x2 matrix C using the standard multiplication formula. The resultant matrix is then printed in a formatted way. Output of the Program yaml Copy Edit Resultant Matrix: 19 22 43 50
0 notes
surajkumasblog · 2 months ago
Text
Armstrong Number in Java - How to Implement It?
I came across the armstrong number in java, where the sum of the cubes (or n-th power) of the digits of a number equals the number itself. For example, 153 is an Armstrong number because:
1³ + 5³ + 3³ = 153
I tried implementing this in Java, but I want to make sure my logic is correct. Here’s my code:
java Copy Edit import java.util.Scanner;
public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); int original = num, sum = 0, digits = 0; int temp = num; while (temp > 0) { temp /= 10; digits++; } temp = num; while (temp > 0) { int digit = temp % 10; sum += Math.pow(digit, digits); temp /= 10; } if (sum == original) System.out.println(num + " is an Armstrong number."); else System.out.println(num + " is not an Armstrong number."); sc.close(); }
} Is there a better way to optimize this, especially for larger numbers? Would recursion be useful here?
0 notes
surajkumasblog · 2 months ago
Text
Matrix Multiplication in Java Using Arrays - Need Help with Output
I’m working on a matrix multiplication in java using arrays using 2D arrays. While I have written the code, I am not sure if my approach is correct. I’d appreciate any corrections or optimizations.
Here’s what I have so far:
java Copy Edit import java.util.Scanner;
public class MatrixMultiplicationArrays { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter rows and columns for first matrix: "); int r1 = sc.nextInt(), c1 = sc.nextInt(); int[][] mat1 = new int[r1][c1]; System.out.print("Enter rows and columns for second matrix: "); int r2 = sc.nextInt(), c2 = sc.nextInt(); int[][] mat2 = new int[r2][c2]; if (c1 != r2) { System.out.println("Matrix multiplication not possible!"); return; } System.out.println("Enter elements of first matrix:"); for (int i = 0; i < r1; i++) for (int j = 0; j < c1; j++) mat1[i][j] = sc.nextInt(); System.out.println("Enter elements of second matrix:"); for (int i = 0; i < r2; i++) for (int j = 0; j < c2; j++) mat2[i][j] = sc.nextInt(); int[][] result = new int[r1][c2]; for (int i = 0; i < r1; i++) for (int j = 0; j < c2; j++) for (int k = 0; k < c1; k++) result[i][j] += mat1[i][k] * mat2[k][j]; System.out.println("Resultant Matrix:"); for (int[] row : result) { for (int val : row) System.out.print(val + " "); System.out.println(); } sc.close();
0 notes
surajkumasblog · 2 months ago
Text
Matrix Multiplication in Java - Need Help!
I am trying to multiply two matrices in Java and need some guidance. I understand that matrix multiplication in java (i.e., the number of columns in the first matrix must equal the number of rows in the second), but I’m struggling to implement it correctly.
Here’s my initial code:
java Copy Edit class MatrixMultiplication { public static void main(String args[]) { int a[][] = { {1, 2, 3}, {4, 5, 6} }; int b[][] = { {7, 8}, {9, 10}, {11, 12} }; int result[][] = new int[a.length][b[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < b[0].length; j++) { for (int k = 0; k < b.length; k++) { result[i][j] += a[i][k] * b[k][j]; } } } for (int[] row : result) { for (int val : row) { System.out.print(val + " "); } System.out.println(); } }
} Does this look correct? Are there any improvements I can make, especially in terms of performance? Any feedback would be appreciated!
0 notes
surajkumasblog · 2 months ago
Text
How to Perform Matrix Multiplication in Java?
I'm currently working on a Java program matrix multiplication java. I understand the basics of matrices, but I'm a bit confused about implementing it efficiently in Java. How do I handle multiplication for different-sized matrices, and what are some best practices to follow?
I've seen nested loops being used for this, but is there a more optimized approach? Also, how do I handle cases where multiplication isn't possible due to dimension mismatches? If anyone has a working example or code snippet, that would be really helpful!
0 notes
surajkumasblog · 2 months ago
Text
Java Count Character Frequency in String – Different Ways
java count character frequency in string:
Using a HashMap (Traditional approach)
Using Java 8 Streams (Modern approach)
Using Arrays for fixed character sets
Here's the HashMap approach:
java
Copy
Edit
import java.util.HashMap;
import java.util.Map;
public class CharFrequency {
public static void main(String[] args) {
String str = "hello world";
Map<Character, Integer> freqMap = new HashMap<>();
for (char ch : str.toCharArray()) {
freqMap.put(ch, freqMap.getOrDefault(ch, 0) + 1);
}
System.out.println(freqMap);
}
}
It efficiently counts character occurrences using a HashMap. The output would be something like:
Copy
Edit
{h=1, e=1, l=3, o=2, w=1, r=1, d=1}
0 notes
surajkumasblog · 2 months ago
Text
Frequency of Characters in a String in Java 8 – Best Methods
I was working with Java 8 and needed to frequency of characters in a string in java 8 . I found a great way using Streams!
java Copy Edit import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors;
public class CharacterFrequency { public static void main(String[] args) { String str = "hello world"; Map<Character, Long> frequencyMap = str.chars() .mapToObj(c -> (char) c) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(frequencyMap); }
} This solution uses Java 8’s functional programming features to count occurrences of each character in the string. Let me know if there are any improvements!
0 notes
surajkumasblog · 2 months ago
Text
Java Program for Armstrong Number – Recursive & Iterative Solutions
java program for armstrong number properties where the sum of their digits raised to the power of their count equals the original number. Here’s both an iterative and a recursive approach:
Iterative Method:
java Copy Edit public class ArmstrongIterative { public static void main(String[] args) { int num = 9474; System.out.println(num + (isArmstrong(num) ? " is " : " is not ") + "an Armstrong number."); }public static boolean isArmstrong(int num) { int temp = num, sum = 0, digits = String.valueOf(num).length(); while (temp > 0) { int digit = temp % 10; sum += Math.pow(digit, digits); temp /= 10; } return sum == num; }
} Recursive Approach:
java Copy Edit public class ArmstrongRecursive { public static void main(String[] args) { int num = 9474; System.out.println(num + (isArmstrong(num, num, (int) Math.log10(num) + 1) ? " is " : " is not ") + "an Armstrong number."); }public static boolean isArmstrong(int num, int temp, int digits) { if (temp == 0) return num == 0; return isArmstrong(num - (int) Math.pow(temp % 10, digits), temp / 10, digits); }
}
0 notes
surajkumasblog · 2 months ago
Text
Java Program to Check Armstrong Number – Simple Approach
I was learning about Armstrong numbers and came up with a java program to check armstrong number or not. Below is a simple approach using a function to validate:
java
Copy
Edit
public class CheckArmstrong { public static void main(String[] args) { int num = 9474; // Change this value to test with different numbers System.out.println(num + (isArmstrong(num) ? " is " : " is not ") + "an Armstrong number."); }
public static boolean isArmstrong(int num) {int sum = 0, temp = num, digits = (int) Math.log10(num) + 1; while (temp > 0) { int digit = temp % 10; sum += Math.pow(digit, digits); temp /= 10; } return sum == num;
}
}
This method calculates the number of digits using logarithms and efficiently determines if the number is Armstrong or not. Let me know if there are any better optimizations!
0 notes
surajkumasblog · 2 months ago
Text
Armstrong Number in Java – Explanation & Example Code
An armstrong number in java, Pluperfect digital invariant, or PDI) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
For example:
153 → 1 3 + 5 3 + 3
3
153 1 3 +5 3 +3 3 =153 (Armstrong Number) 9474 → 9 4 + 4 4 + 7 4 + 4
4
9474 9 4 +4 4 +7 4 +4 4 =9474 (Armstrong Number) 123 → 1 3 + 2 3 + 3
3
36 1 3 +2 3 +3 3 =36 (Not an Armstrong Number) Java Code to Check Armstrong Number java Copy Edit import java.util.Scanner;
public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); sc.close(); if (isArmstrong(num)) { System.out.println(num + " is an Armstrong number."); } else { System.out.println(num + " is not an Armstrong number."); } } static boolean isArmstrong(int num) { int original = num, sum = 0, digits = String.valueOf(num).length(); while (num > 0) { int digit = num % 10; sum += Math.pow(digit, digits); num /= 10; } return sum == original; }
} This program takes an input from the user, calculates the sum of digits raised to their respective power, and determines whether it is an Armstrong number.
0 notes
surajkumasblog · 2 months ago
Text
0 notes
surajkumasblog · 2 months ago
Text
0 notes
surajkumasblog · 2 months ago
Text
0 notes