Comp 1
Comp 1
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
Output:
$ javac Encoding_Matrix.java
$ java Encoding_Matrix
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
char c = 'a';
int count = 0;
// First Approach
for(char ch : charArray)
if(ch == c)
count++;
}
System.out.println("Total occurrence of character 'a' using 1st approach
= "+count);
// Second Approach
Output :
class SearchNumber
if(numbers[i] == searchNumber)
isExist = true;
pos = i;
break;
}
if(isExist)
else
Output :
class Power {
public static void main(String[] args) {
Output:
Java Program to Check Whether a Number can be Expressed as
// 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.");
}
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
class Main {
// binary number
long num = 110110111;
System.out.println("Binary to Decimal");
System.out.println(num + " = " + decimal);
}
while (num != 0) {
remainder = num % 10;
num /= 10;
decimalNumber += remainder * Math.pow(2, i);
++i;
}
return decimalNumber;
}
}
Run Code
Output
++low;
}
}
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
1 3 7
1 3 1
1 1 3
import java.util.Scanner;
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
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();
}
}
}