Java Program to Find the Frequency of Odd and
Even Numbers in Matrix
1. import java.util.Scanner;
2. public class Frequency
3. {
4. public static void main(String[] args)
5. {
6. int p, q, count1 = 0, count2 = 0;
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter number of rows in matrix:");
9. p = s.nextInt();
10. System.out.print("Enter number of columns in matrix:");
11. q = s.nextInt();
12. int a[][] = new int[p][q];
13. System.out.println("Enter all the elements of matrix:");
14. for (int i = 0; i < p; i++)
15. {
16. for (int j = 0; j < q; j++)
17. {
18. a[i][j] = s.nextInt();
19. }
20. }
21. System.out.println("Given Matrix:");
22. for (int i = 0; i < p; i++)
23. {
24. for (int j = 0; j < q; j++)
25. {
26. System.out.print(a[i][j] + " ");
27. }
28. System.out.println("");
29. }
30. for (int i = 0; i < p; i++)
31. {
32. for (int j = 0; j < q; j++)
33. {
34. if((a[i][j] % 2) == 0)
35. {
36. count1++;
37. }
38. else
39. {
40. count2++;
41. }
42. }
43. }
44. System.out.println("Even number
frequency:"+count1);
45. System.out.println("Odd number frequency:"+count2);
46. }
47.}
Output:
$ javac Frequency.java
$ java Frequency
Enter number of rows in matrix:3
Enter number of columns in matrix:3
Enter all the elements of matrix:
1
2
3
4
5
6
7
8
9
Given Matrix:
1 2 3
4 5 6
7 8 9
Even number frequency:4
Odd number frequency:5
Java Program to Perform Message Encoding
Using Matrix Multiplication
1. //This is sample program to encode any 2-dimensional matrix using
matrix of elememts (i+j)
2. // for 2x2 encoding is done by multiplying given matrix with 0 1
3. // 1 2
4. import java.util.Scanner;
5.
6. public class Encoding_Matrix
7. {
8. public static void main(String args[])
9. {
10. int n;
11. Scanner input = new Scanner(System.in);
12. System.out.println("Enter the base of squared matrices");
13. n = input.nextInt();
14. int [][] a = new int[n][n];
15. int [][] b = new int[n][n];
16. int [][] c = new int[n][n];
17. System.out.println("Enter the elements of matrix to be encoded: ");
18. for(int i=0; i<n; i++)
19. for(int j=0; j<n; j++)
20. a[i][j] = input.nextInt();
21.
22. for(int i=0; i<n; i++)
23. for(int j=0; j<n; j++)
24. b[i][j] = i+j;
25.
26. for (int i = 0; i < n; i++)
27. {
28. for (int j = 0; j < n; j++)
29. {
30. for (int k = 0; k < n; k++)
31. {
32. c[i][j] = c[i][j] + a[i][k] * b[k][j];
33. }
34. }
35. }
36.
37. System.out.println("The Encoded matrix is:");
38. for(int i=0; i<n; i++)
39. {
40. for(int j=0; j<n; j++)
41. {
42. System.out.print(c[i][j] + " ");
43. }
44. System.out.println();
45. }
46. input.close();
47. }
48. }
Output:
$ javac Encoding_Matrix.java
$ java Encoding_Matrix
Enter the base of squared matrices
2
Enter the elements of matrix to be encoded:
15
39
The Encoded matrix is:
5 11
9 21
Enter the base of squared matrices
3
Enter the elements of matrix to be encoded:
123
456
789
The Encoded matrix is:
8 14 20
17 32 47
26 50 74
Java Program to Check if it is a Sparse Matrix
1. //This is a sample program to check whether the matrix is sparse
matrix or not
2. //The complexity of the code is O(n^2)
3. import java.util.Scanner;
4.
5. public class Sparsity_Matrix
6. {
7. public static void main(String args[])
8. {
9. Scanner sc = new Scanner(System.in);
10. System.out.println("Enter the dimensions of the matrix: ");
11. int m = sc.nextInt();
12. int n = sc.nextInt();
13. double[][] mat = new double[m][n];
14. int zeros = 0;
15. System.out.println("Enter the elements of the matrix: ");
16. for(int i=0; i<m; i++)
17. {
18. for(int j=0; j<n; j++)
19. {
20. mat[i][j] = sc.nextDouble();
21. if(mat[i][j] == 0)
22. {
23. zeros++;
24. }
25. }
26. }
27.
28. if(zeros > (m*n)/2)
29. {
30. System.out.println("The matrix is a sparse matrix");
31. }
32. else
33. {
34. System.out.println("The matrix is not a sparse matrix");
35. }
36.
37. sc.close();
38. }
39. }
Output:
$ javac Sparsity_matrix.java
$ java Sparsity_matrix
Enter the dimensions of the matrix:
23
Enter the elements of the matrix:
100
211
The matrix is not a sparse matrix
$ javac Sparsity_matrix.java
$ java Sparsity_matrix
Enter the dimensions of the matrix:
34
Enter the elements of the matrix:
1000
0100
0011
The matrix is a sparse matrix
Program to find occurrence of a character in a string in Java
class CharacterCount
public static void main(String[] args)
String str = "Java is easy to learn";
char c = 'a';
int count = 0;
// First Approach
char[] charArray = str.toCharArray();
for(char ch : charArray)
if(ch == c)
count++;
}
System.out.println("Total occurrence of character 'a' using 1st approach
= "+count);
// Second Approach
int count2 = str.length() - str.replace("a", "").length();
System.out.println("Total occurrence of character 'a' using 2nd
approach = "+count2);
Output :
Total occurrence of character 'a' using 1st approach = 4
Total occurrence of character 'a' using 2nd approach = 4
Program to search an element in an array
class SearchNumber
public static void main(String [] args)
int [] numbers = {40,60,80,65,70};
boolean isExist = false;
int searchNumber = 80;
int pos = -1;
for(int i = 0; i < numbers.length; i++)
if(numbers[i] == searchNumber)
isExist = true;
pos = i;
break;
}
if(isExist)
System.out.println("number "+searchNumber+" exist in the array at
index = "+pos);
else
System.out.println("number "+searchNumber+" does not exist inside
the array");
Output :
number 80 exist in the array at index = 2
Program to calculate power using recursion
class Power {
public static void main(String[] args) {
int base = 3, powerRaised = 4;
int result = power(base, powerRaised);
System.out.println(base + "^" + powerRaised + "=" + result);
}
public static int power(int base, int powerRaised) {
if (powerRaised != 0) {
// recursive call to power()
return (base * power(base, powerRaised - 1));
}
else {
return 1;
}
}
}
Output:
Java Program to Check Whether a Number can be Expressed as
Sum of Two Prime Numbers
public class Main {
public static void main(String[] args) {
int number = 34;
boolean flag = false;
for (int i = 2; i <= number / 2; ++i) {
// condition for i to be a prime number
if (checkPrime(i)) {
// condition for n-i to be a prime number
if (checkPrime(number - i)) {
// n = primeNumber1 + primeNumber2
System.out.printf("%d = %d + %d\n", number, i, number - i);
flag = true;
}
}
}
if (!flag)
System.out.println(number + " cannot be expressed as the sum of two
prime numbers.");
}
// Function to check prime number
static boolean checkPrime(int num) {
boolean isPrime = true;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
}
Output:
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
Java Program to Check Whether a Number can be Expressed as
Sum of Two Prime Numbers
class Main {
public static void main(String[] args) {
// binary number
long num = 110110111;
// call method by passing the binary number
int decimal = convertBinaryToDecimal(num);
System.out.println("Binary to Decimal");
System.out.println(num + " = " + decimal);
}
public static int convertBinaryToDecimal(long num) {
int decimalNumber = 0, i = 0;
long remainder;
while (num != 0) {
remainder = num % 10;
num /= 10;
decimalNumber += remainder * Math.pow(2, i);
++i;
}
return decimalNumber;
}
}
Run Code
Output
110110111 in binary = 439 in decimal
Java Program to Display Prime Numbers Between
Intervals Using Function
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
if(checkPrimeNumber(low))
System.out.print(low + " ");
++low;
}
}
public static boolean checkPrimeNumber(int num) {
boolean flag = true;
for(int i = 2; i <= num/2; ++i) {
if(num % i == 0) {
flag = false;
break;
}
}
return flag;
}
}
Output
23 29 31 37 41 43 47
Write a program to declare a single-dimensional array a[] and
a square matrix b[][] of size N, where N > 2 and N < 10. Allow the
user to input positive integers into the single dimensional array.
Example :-
INPUT: N = 3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT: SORTED ARRAY: 1 3 7
FILLED MATRIX
1 3 7
1 3 1
1 1 3
import java.util.Scanner;
public class Array
{
public static void sortArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = in.nextInt();
if (n <= 2 || n >= 10) {
System.out.println("MATRIX SIZE OUT OF RANGE");
return;
}
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF SINGLE DIMENSIONAL
ARRAY:");
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
for (int i = n - 1, r = 0; i >= 0; i--, r++) {
for (int j = 0; j <= i; j++) {
b[r][j] = a[j];
}
for (int k = n - 1; k > i; k--) {
b[r][k] = a[k - i - 1];
}
}
System.out.println();
System.out.println("FILLED MATRIX:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}