Java Practice Programs
Java Practice Programs
8) Java Program to find the frequency of odd & even numbers in the given matrix
10) Java Program to find the sum of each row and each column of a matrix
In this program, we need to count the number of characters present in the string:
To count the number of characters present in the string, we will iterate through the string and
count the characters. In above example, total numbers of characters present in the string are 19.
Algorithm
o STEP 1: START
o STEP 6: i=i+1
o STEP 8: END
Program:
1. public class CountCharacter
2. {
5. int count = 0;
6.
10. count++;
11. }
12.
13. //Displays the total number of characters present in the given string
15. }
16. }
Output:
4) Java Program to count the total number of vowels and consonants in a string
Java Program to count the total number of vowels and consonants in a string
In this program, our task is to count the total number of vowels and consonants present in the given
string.
As we know that, the characters a, e, i, o, u are known as vowels in the English alphabet. Any
character other than that is known as the consonant.
To solve this problem, First of all, we need to convert every upper-case character in the string to
lower-case so that the comparisons can be done with the lower-case vowels only not upper-case
vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match
each character with all the vowels, i.e., a, e, i, o, u. If the match is found, increase the value of count
by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given
below.
Algorithm
o STEP 1: START
o STEP 2: SET vCount =0, cCount =0
o STEP 9: i = i + 1
Program:
3.
6.
7. //Declare a string
9.
12.
18. }
22. cCount++;
23. }
24. }
27. }
28. }
Output:
Number of vowels: 10
Number of consonants: 17
13) Program:
14) import java.util.Arrays;
15) public class Anagram {
16) public static void main (String [] args) {
17) String str1="Brag";
18) String str2="Grab";
19)
20) //Converting both the string to lower case.
21) str1 = str1.toLowerCase();
22) str2 = str2.toLowerCase();
23)
24) //Checking for the length of strings
25) if (str1.length() != str2.length()) {
26) System.out.println("Both the strings are not anagram");
27) }
28) else {
29) //Converting both the arrays to character array
30) char[] string1 = str1.toCharArray();
31) char[] string2 = str2.toCharArray();
32)
33) //Sorting the arrays using in-built function sort ()
34) Arrays.sort(string1);
35) Arrays.sort(string2);
36)
37) //Comparing both the arrays using in-built function equals ()
38) if(Arrays.equals(string1, string2) == true) {
39) System.out.println("Both the strings are anagram");
40) }
41) else {
42) System.out.println("Both the strings are not anagram");
43) }
44) }
45) }
46) }
47) Output:
48) Both the strings are anagram
49)
To check whether the string can be divided into N equal parts, we need to divide the length of the
string by n and assign the result to variable chars.
If the char comes out to be a floating point value, we can't divide the string otherwise run for loop to
traverse the string and divide the string at every chars interval.
Algorithm
o STEP 1: START
o STEP 4: SET n =3
o STEP 8: IF (len%n!=0)
then PRINT ("String can't be divided into equal parts")
else go to STEP 9
o STEP 18: i = i + 1
Program:
4.
7. //n determines the variable that divide the string in 'n' equal parts
8. int n = 3;
13. if(len % n != 0) {
14. System.out.println("Sorry this string cannot be divided into "+ n +" equal parts.");
15. }
16. else {
21. temp++;
22. }
25. System.out.println(equalStr[i]);
26. }
27. }
28. }
29. }
Output:
aaaa
bbbb
cccc
In this program, all the subsets of the string need to be printed. The subset of a string is the character
or the group of characters that are present inside the string.
For example, all possible subsets of a string "FUN" will be F, U, N, FU, UN, FUN.
To complete this program, we need to run two for loops. The outer loop is used to maintain the
relative position of the first character and the second loop is used to create all possible subsets and
prints them one by one.
Algorithm
o STEP 1: START
o STEP 10: j =j + 1
o STEP 11: i =i +1
o STEP 12: PRINT ("All subsets for given string are: ")
Program:
1. public class AllSubsets {
2. public static void main(String[] args) {
3.
4. String str = "FUN";
5. int len = str.length();
6. int temp = 0;
7. //Total possible subsets for string of size n is n*(n+1)/2
8. String arr[] = new String[len*(len+1)/2];
9.
10. //This loop maintains the starting character
11. for(int i = 0; i < len; i++) {
12. //This loop adds the next character every iteration for the subset t
o form and add it to the array
13. for(int j = i; j < len; j++) {
14. arr[temp] = str.substring(i, j+1);
15. temp++;
16. }
17. }
18.
19. //This loop prints all the subsets formed from the string.
20. System.out.println("All subsets for given string are: ");
21. for(int i = 0; i < arr.length; i++) {
22. System.out.println(arr[i]);
23. }
24. }
25.}
Output:
All subsets for given string are:
FU
FUN
UN
The spiral pattern (or matrix in spiral form) is frequently asked in Java interviews and academics. In
this section, we will create a Java program to create a spiral pattern or spiral matrix.
A spiral pattern is a number pattern that can be represented in matrix form. It is made up of a 2D
array (m*n). Pattern may represent in two forms, either clockwise or anticlockwise.
In order to print a matrix in spiral pattern (clockwise) form, we need to follow the following traversal
order:
In order to print the spiral pattern (anticlockwise), reverse the above traversal order.
Let's understand the pictorial representation of the spiral patterns, as shown in the following figure.
Answers:
Java program
2.
5432*
543*1
54*21
5*321
*4321
*000*000*
0*00*00*0
00*0*0*00
000***000
1. public class pattern
2. {
3. public static void main(String[] args){
4. int lines=4;
5. int i,j;
6. for(i=1;i<=lines;i++){// this loop is used to print lines
7. for(j=1;j<=lines;j++){// this loop is used to print * in a line
8. if(i==j)
9. System.out.print("*");
10. else
11. System.out.print("0");
12. }
13. j--;
14. System.out.print("*");
15. while(j>=1){// this loop is used to print * in a line
16. if(i==j)
17. System.out.print("*");
18. else
19. System.out.print("0");
20. j--;
21. }
22. System.out.println("");
23. }
24. }
25. }
Output:
*000*000*
0*00*00*0
00*0*0*00
000***000
1
24
369
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
24
369
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
Algorithm:
o STEP 1: START
o STEP 3: DEFINE i, j
o STEP 8: j = j + 1
o STEP 9: j = j - 2
o STEP 12: j = j - 1
o STEP 14: i = i + 1
o STEP 15: END
Program:
12344321
123**321
12****21
1******1
Algorithm:
o STEP 1: START
o STEP 3: DEFINE i, j.
Program:
12344321
123**321
12****21
1******1
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
Algorithm:
o STEP 1: START
o STEP 8: PRINT 0
o STEP 15: i = i + 1
Program:
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
A
BB
CCC
DDDD
EEEEE
To accomplish this task, we need to use two loops, the first loop is responsible for printing the line
breaks whereas the second loop is responsible for printing the alphabet.
Algorithm:
o STEP 1: START
o STEP 8: END
Program:
1. class pattern
2. {
3. public static void main(String[] args)
4. {
5. int n = 4;
6. for(int i = 0 ; i <= n ; i++)
7. {
8. for(int j = 0 ; j <= i ; j++)
9. {
10. System.out.print(" "+(char)(65 + i));
11. }
12. System.out.println("");
13. }
14. }
15.}
Output:
BB
CCC
DDDD
EEEEE
To accomplish this task, we need to create two loops and the 2nd loop is to be executed according to
the first loop. The first loop is responsible for printing the line breaks whereas the second loop is
responsible for printing the stars (*).
Algorithm:
o STEP 1: START
o STEP 9: END
Program:
1. class pattern
2.
3. public static void main(String[] args)
4. {
5. int i,j, n=7;
6. System.out.println("Right angle triangle");
7. for(i=1;i<n;i++)
8. {
9. for(j=1;j<=i;j++)
10. {
11. System.out.print(" *");
12. }
13. System.out.println("");
14. }
15. }
Output:
9. Java program to print the following pattern on the console
Algorithm:
o STEP 1: START
o STEP 4: if(i==0 or i==10 or j==0 or j==10) then PRINT 1 else PRINT ""
o STEP 8: END
Program:
12345
1234
123
12
1
Algorithm:
o STEP 1: START
o STEP 2: DEFINE i, j.
o STEP 8: END
Program:
1. public class pattern
2. {
3. public static void main(String[] args)
4. { int i ,j;
5. int n = 5;
6. for(i = n; i>0 ; i-- )
7. {
8. for(j = 1; j<=i ; j++)
9. {
10. System.out.print(j);
11. }
12. System.out.println("");
13. }
14. }
15.}
Output:
12345
1234
123
12