0% found this document useful (0 votes)
1 views

Java Practice Programs

The document provides a comprehensive list of Java practice programs categorized into various sections including Basic Programs, Number Programs, Array Programs, Matrix Programs, and String Programs. Each section contains specific programming tasks such as generating Fibonacci series, checking for prime numbers, and manipulating strings and arrays. The document also includes algorithms and sample code for each program to aid in understanding and implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java Practice Programs

The document provides a comprehensive list of Java practice programs categorized into various sections including Basic Programs, Number Programs, Array Programs, Matrix Programs, and String Programs. Each section contains specific programming tasks such as generating Fibonacci series, checking for prime numbers, and manipulating strings and arrays. The document also includes algorithms and sample code for each program to aid in understanding and implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Java Practice Programs

Java Basic Programs


1) Fibonacci Series in Java

2) Prime Number Program in Java

3) Palindrome Program in Java

4) Factorial Program in Java

5) Armstrong Number in Java

6) How to Generate Random Number in Java

Java Number Programs


1) How to Reverse a Number in Java

2) Java Program to convert Number to Word

3) Automorphic Number Program in Java

4) Peterson Number in Java

5) Sunny Number in Java

6) Tech Number in Java

7) Fascinating Number in Java

8) Keith Number in Java

9) Neon Number in Java

10) Spy Number in Java

Java Array Programs


1) Java Program to copy all elements of one array into another array

2) Java Program to find the frequency of each element in the array

3) Java Program to left rotate the elements of an array

4) Java Program to print the duplicate elements of an array

5) Java Program to print the elements of an array

6) Java Program to print the elements of an array in reverse order

7) Java Program to print the elements of an array present on even position

8) Java Program to print the elements of an array present on odd position

9) Java Program to print the largest element in an array


10) Java Program to print the smallest element in an array

Java Matrix Programs


1) Java Matrix Programs

2) Java Program to Add Two Matrices

3) Java Program to Multiply Two Matrices

4) Java Program to subtract the two matrices

5) Java Program to determine whether two matrices are equal

6) Java Program to display the lower triangular matrix

7) Java Program to display the upper triangular matrix

8) Java Program to find the frequency of odd & even numbers in the given matrix

9) Java Program to find the product of two matrices

10) Java Program to find the sum of each row and each column of a matrix

Java String Programs


1) Java Program to count the total number of characters in a string

Java Program to count the total number of characters in a string

In this program, we need to count the number of characters present in the string:

The best of both worlds

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.

For programming, follow the algorithm given below:

Algorithm

o STEP 1: START

o STEP 2: DEFINE String string = "The best of both worlds".

o STEP 3: SET count =0.

o STEP 4: SET i=0. REPEAT STEP 5 to STEP 6 UNTIL i<string.length

o STEP 5: IF (string.charAt(i)!= ' ') then count =count +1.

o STEP 6: i=i+1

o STEP 7: PRINT count.

o STEP 8: END

Program:
1. public class CountCharacter

2. {

3. public static void main(String[] args) {

4. String string = "The best of both worlds";

5. int count = 0;

6.

7. //Counts each character except space

8. for(int i = 0; i < string.length(); i++) {

9. if(string.charAt(i) != ' ')

10. count++;

11. }

12.

13. //Displays the total number of characters present in the given string

14. System.out.println("Total number of characters in a string: " + count);

15. }

16. }

Output:

Total number of characters in a string: 19

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 3: DEFINE string str = "This is a really simple sentence".

o STEP 4: CONVERT str to lowercase

o STEP 5: SET i =0.

o STEP 6: REPEAT STEP 6 to STEP 8 UNTIL i<str.length()

o STEP 7: IF any character of str matches with any vowel then


vCount = vCount + 1.
STEP 8: IF any character excepting vowels lies BETWEEN a and z then
cCount = cCount =+1.

o STEP 9: i = i + 1

o STEP 10: PRINT vCount.

o STEP 11: PRINT cCount.

o STEP 12: END

Program:

1. public class CountVowelConsonant {

2. public static void main(String[] args) {

3.

4. //Counter variable to store the count of vowels and consonant

5. int vCount = 0, cCount = 0;

6.

7. //Declare a string

8. String str = "This is a really simple sentence";

9.

10. //Converting entire string to lower case to reduce the comparisons

11. str = str.toLowerCase();

12.

13. for(int i = 0; i < str.length(); i++) {

14. //Checks whether a character is a vowel

15. if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || s


tr.charAt(i) == 'u') {

16. //Increments the vowel counter


17. vCount++;

18. }

19. //Checks whether a character is a consonant

20. else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {

21. //Increments the consonant counter

22. cCount++;

23. }

24. }

25. System.out.println("Number of vowels: " + vCount);

26. System.out.println("Number of consonants: " + cCount);

27. }

28. }

Output:

Number of vowels: 10

Number of consonants: 17

2) Java Program to determine whether two strings are the anagram


Java Program to determine whether two strings are the anagram
Two Strings are called the anagram if they contain the same characters. However, the order
or sequence of the characters can be different.
In this program, our task is to check for two strings that, they are the anagram or not. For
this purpose, we are following a simpler approach.
First of all, Compare the length of the strings, if they are not equal in the length then print
the error message and make an exit, otherwise, convert the string into lower-case for the
easy comparisons. Sort both the strings using bubble sort or other sorting methods. If the
strings are found to be identical after sorting, then print that strings are anagram otherwise
print that strings are not the anagram.
For programming, follow the algorithm given below:
3) Algorithm
4) STEP 1: START
5) STEP 2: DEFINE str1 = "Brag", str2 = "Grab".
6) STEP 3: CONVERT str1, str2 to lower-case.
7) STEP 4: IF length of str1, str2 are not equal then PRINT "Not Anagram"
else go to Step 5
8) STEP 5: CONVERT str1, str2 to character arrays.
9) STEP 6: SORT the arrays.
10) STEP 7: COMPARE the arrays, IF equal then PRINT "Anagram"
else
PRINT "Not Anagram"
11) STEP 8: END
12)

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)

6) Java Program to divide a string in 'N' equal parts.

Java Program to divide a string in 'N' equal parts.


Here, our task is to divide the string S into n equal parts. We will print an error message if the string
cannot be divisible into n equal parts otherwise all the parts need to be printed as the output of the
program.

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.

The algorithm of the program is given below.

Algorithm

o STEP 1: START

o STEP 2: DEFINE str = "aaaabbbbcccc"

o STEP 3: DEFINE len

o STEP 4: SET n =3

o STEP 5: SET temp = 0.

o STEP 6: chars = len/n

o STEP 7: DEFINE String[] equalstr.

o STEP 8: IF (len%n!=0)
then PRINT ("String can't be divided into equal parts")
else go to STEP 9

o STEP 9: SET i =0.

o STEP 10: REPEAT STEP 11 to STEP 14 UNTIL i<len

o STEP 11: DEFINE substring part.

o STEP 12: equalstr [temp] = part

o STEP 13: temp = temp + 1

o STEP 14: i = i + chars

o STEP 15: PRINT n

o STEP 16: SET i=0. REPEAT STEP 17 to STEP 18 UNTIL i<equalstr.length

o STEP 17: PRINT equalstr[i]

o STEP 18: i = i + 1

o STEP 19: END

Program:

1. public class DivideString {


2. public static void main(String[] args) {

3. String str = "aaaabbbbcccc";

4.

5. //Stores the length of the string

6. int len = str.length();

7. //n determines the variable that divide the string in 'n' equal parts

8. int n = 3;

9. int temp = 0, chars = len/n;

10. //Stores the array of string

11. String[] equalStr = new String [n];

12. //Check whether a string can be divided into n equal parts

13. if(len % n != 0) {

14. System.out.println("Sorry this string cannot be divided into "+ n +" equal parts.");

15. }

16. else {

17. for(int i = 0; i < len; i = i+chars) {

18. //Dividing string in n equal part using substring()

19. String part = str.substring(i, i+chars);

20. equalStr[temp] = part;

21. temp++;

22. }

23. System.out.println(n + " equal parts of given string are ");

24. for(int i = 0; i < equalStr.length; i++) {

25. System.out.println(equalStr[i]);

26. }

27. }

28. }

29. }

Output:

3 equal parts of given string are

aaaa
bbbb

cccc

7) Java Program to find all subsets of a string

Java Program to find all subsets of a string

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.

All the possible subsets for a string will be n(n+1)/2.

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.

The algorithm of the program is given below.

Algorithm

o STEP 1: START

o STEP 2: DEFINE string str = "FUN"

o STEP 3: DEFINE len = str.length()

o STEP 4: SET temp =0

o STEP 5: DEFINE String array having length: len*(len + 1)/2

o STEP 6: SET i =0. REPEAT STEP 7 to STEP 11 UNTIL i<len

o STEP 7: SET j =1. REPEAT STEP 8 to STEP 10 UNTIL j<len

o STEP 8: arr[temp] = str.substring(i, j+1)

o STEP 9: temp = temp + 1

o STEP 10: j =j + 1

o STEP 11: i =i +1

o STEP 12: PRINT ("All subsets for given string are: ")

o STEP 13: SET i = 0

o STEP 14: REPEAT STEP 14 UNTIL i<arr.length

o STEP 14: PRINT arr[i]

o STEP 15: END

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

Java Pattern programs


Java Program to Print Spiral Pattern

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.

What is Spiral Matrix or Spiral Pattern?

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:

o Left to right (first row)

o Top to bottom (Last column)

o Right to left (last row)

o Bottom to top (First column)

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

1. 1. public class SpiralPatternExample1


2. {
3. //defining method to print the spiral pattern or matrix
4. static void printSpiralPattern(int size)
5. {
6. //create two variables row and col to traverse rows and columns
7. int row = 0, col = 0;
8. int boundary = size - 1;
9. int sizeLeft = size - 1;
10.int flag = 1;
11.//variables r, l, u and d are used to determine the movement
12.// r = right, l = left, d = down, u = upper
13.char move = 'r';
14.//creating a 2D array for matrix
15.int[][] matrix =new int [size][size];
16.for (int i = 1; i < size * size + 1; i++)
17.{
18. //assigning values
19. matrix[row][col] = i;
20.//switch-case to determine the next index
21.switch (move)
22.{
23.//if right, go right
24.case 'r':
25. col += 1;
26. break;
27.//if left, go left
28.case 'l':
29. col -= 1;
30. break;
31.//if up, go up
32.case 'u':
33. row -= 1;
34. break;
35.//if down, go down
36.case 'd':
37. row += 1;
38. break;
39.}
40.//checks if the matrix has reached the array boundary
41.if (i == boundary)
42. {
43. //adds the left size for the next boundary
44. boundary = boundary + sizeLeft;
45. //decrease the size left by 1, if 2 rotations have been made
46. if (flag != 2)
47. {
48. flag = 2;
49. }
50. else
51. {
52. flag = 1;
53. sizeLeft -= 1;
54. }
55. //switch-case to rotate the movement
56. switch (move)
57. {
58. //if right, rotate to down
59. case 'r':
60. move = 'd';
61. break;
62. // if down, rotate to left
63. case 'd':
64. move = 'l';
65. break;
66. // if left, rotate to up
67. case 'l':
68. move = 'u';
69. break;
70. // if up, rotate to right
71. case 'u':
72. move = 'r';
73. break;
74. }
75. }
76. }
77.//printing the spiral matrix or pattern
78.//outer for loop for rows
79. for (row = 0; row < size; row++)
80. {
81. //inner for loop for columns
82. for (col = 0; col < size; col++)
83. {
84. int n = matrix[row][col];
85. if(n < 10)
86. System.out.print(n +" ");
87. else
88. System.out.print(n +" ");
89. }
90. System.out.println();
91. }
92.}
93.//driver Code
94.public static void main(String args[])
95.{
96.//size of the array?s row and column
97.int size = 5;
98.System.out.println("Spiral Matrix or Pattern is: \n");
99.//calling the method that prints the spiral pattern or matrix
100. printSpiralPattern(size);
101. }
102. }
Output:
Let's see another spiral pattern.

2.

2. Java program to print the following pattern

5432*
543*1
54*21
5*321
*4321

1. public class Main{


2. public static void main(String []args){
3. int i,j,lines=5;
4. for(i=1;i<=lines;i++){// this loop is used to print the lines
5. for(j=lines;j>=1;j--){// this loop is used to print numbers in a line
6. if(j!=i)
7. System.out.print(j);
8. else
9. System.out.print("*");
10. }
11. System.out.println("");
12. }
13. }}
3. Java program to print the following pattern

*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

4. Java program to print the following pattern

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

1. public class pattern{


2. public static void main(String[] args){
3. int lines=10;
4. int i=1;
5. int j;
6. for(i=1;i<=lines;i++){// this loop is used to print the lines
7. for(j=1;j<=i;j++){// this loop is used to print lines
8. System.out.print(i*j+" ");
9. }
10.System.out.println("");
11.}
12.}
13.}
Output:

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

5. Java program to print the following pattern

Algorithm:

o STEP 1: START

o STEP 2: SET lines=8

o STEP 3: DEFINE i, j

o STEP 4: SET i=1.REPEAT STEP 5 to 14 UNTIL i<lines< li="" style="box-sizing:


border-box;"></lines<>

o STEP 5: SET j=1

o STEP 6: REPEAT STEP 7 and 8 UNTIL j <=(lines/2)

o STEP 7: IF j is equals to i PRINT j


ELSE IF i is greater than 4 and j equals (lines-i) PRINT j
ELSE PRINT " "

o STEP 8: j = j + 1

o STEP 9: j = j - 2

o STEP 10: REPEAT STEP 11 and 12 UNTIL j > 0

o STEP 11: IF j is equals to i PRINT j


ELSE IF i is greater than 4 and j equals (lines-i) then PRINT j
ELSE PRINT " "

o STEP 12: j = j - 1

o STEP 13: PRINT a new line

o STEP 14: i = i + 1
o STEP 15: END

Program:

1. public class pattern{


2. public static void main(String []args){
3.
4. int lines=8;
5. int i,j;
6. for(i=1;i<lines;i++){// this loop is used to print the lines
7. for(j=1;j<=lines/2;j++){// this loop is used to print numbers
8. if(i==j){
9. System.out.print(j);
10. }
11. else if(i>4 && j==lines-i){
12. System.out.print(j);
13. }
14. else{
15. System.out.print(" ");
16. }
17. }
18. j=j-2;
19. while(j>0){ //this loop is used to print numbers
20. if(i==j){
21. System.out.print(j);
22. }
23. else if(i>4 && j==lines-i){
24. System.out.print(j);
25. }
26. else{
27. System.out.print(" ");
28. }
29.
30. j--;
31. }
32. System.out.println("");
33. }
34. }
35.}
Output:

5. Java program to print the following pattern on the console

12344321
123**321
12****21
1******1

Algorithm:

o STEP 1: START

o STEP 2: SET lines=4

o STEP 3: DEFINE i, j.

o STEP 4: SET space=0

o STEP 5: SET i=0

o STEP 6: REPEAT STEP 7 TO 20 UNTIL i<lines< li="" style="box-sizing: border-box;"></lines<>


o STEP 7: SET j=1

o STEP 8: REPEAT STEP 9 UNTIL j <= space.

o STEP 9: PRINT " " and SET J=J+1

o STEP 10: SET j=1

o STEP 11: REPEAT STEP 12 and 13 UNTIL j<=lines

o STEP 12: IF j <=(lines-i). PRINT j ELSE PRINT *

o STEP 13: SET j=j+1

o STEP 14: DECREMENT j by 1

o STEP 15: REPEAT STEP 16 and 17 UNTIL j>0

o STEP 16: IF j>( lines-i ) then PRINT * ELSE PRINT j

o STEP 17: SET j=j-1

o STEP 18: IF (lines-i) >9 INCREMENT space by 1

o STEP 19: PRINT new line

o STEP 20: SET i=i+1

o STEP 21: END

Program:

1. public class pattern


2. {
3. public static void main(String[] args) {
4. int lines=4;
5. int i,j;
6. int space=0;
7. for(i=0;i<lines;i++){// this loop is used to print lines
8. for(j=1;j<=space;j++){// this loop is used to print space in a line
9. System.out.print(" ");
10. }
11. for(j=1;j<=lines;j++){// this loop is used to print numbers in a line
12. if(j<=(lines-i))
13. System.out.print(j);
14. else
15. System.out.print("*");
16. }
17. j--;
18. while(j>0){// this loop is used to print numbers in a line
19. if(j>lines-i)
20. System.out.print("*");
21. else
22. System.out.print(j);
23. j--;
24. }
25. if((lines-i)>9)// this loop is used to increment space
26. space=space+1;
27. System.out.println("");
28. }
29.}
Output:

12344321

123**321

12****21

1******1

6. Java program to print the following pattern on the console

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

Algorithm:

o STEP 1: START

o STEP 2: SET lines=10


SET i=1

o STEP 3: REPEAT STEP 4 to 15 UNTIL i<=lines

o STEP 4: SET count =0


SET j=1

o STEP 5: REPEAT STEP 6 and 7 UNTIL j<lines< li="" style="box-sizing: border-box;"></lines<>

o STEP 6: IF count is less than (i-1)


IF j is not less than (lines-i+1)
PRINT j AND INCREMENT count by 1

o STEP 7: SET j=j+1

o STEP 8: PRINT 0

o STEP 9: SET count =0

o STEP 10: DECREMENT j by 1

o STEP 11: REPEAT STEP 12 and 13 UNTIL j is greater than 0

o STEP 12: IF count is less than (i-1)


PRINT j AND INCREMENT count by 1

o STEP 13: SET j = j - 1

o STEP 14: PRINT new line

o STEP 15: i = i + 1

o STEP 16: END

Program:

1. public class pattern{


2. public static void main(String []args){
3. int lines=10;
4. int i,j;
5. int count;
6. for(i=1;i<=lines;i++){// this loop is used to print lines
7. count=0;
8. for(j=1;j<lines;j++){// this loop is used to print numbers in a line
9. if(count<i-1){
10. if(!(j<lines-i+1)){
11. System.out.print(j);
12. count++;
13. }
14. }
15. }
16. System.out.print("0");
17. count=0;
18. for(--j;j>=1;j--){// this loop is used to print j and increment count
19. if(count<i-1){
20. System.out.print(j);
21. count++;
22. }
23. }
24. System.out.println("");
25. }
26. }
27.}
Output:

909

89098

7890987
678909876

56789098765

4567890987654

345678909876543

23456789098765432

1234567890987654321

7. Java program to print the following pattern on the console

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 2: SET n=4.

o STEP 3: SET i=0.

o STEP 4: REPEAT STEP 5 to STEP 7 UNTIL i<=n

o STEP 5: SET j = 0. REPEAT STEP 6 UNTIL j<=i.

o STEP 6: PRINT char(65+i) and SET j=j+1.

o STEP 7: PRINT new line and SET i=i+1.

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

8. Java program to print the following pattern on the console

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 2: DEFINE i,j

o STEP 3: SET n=7


o STEP 4: PRINT "Right Angle Triangle"

o STEP 5: SET i=1.REPEAT STEP 6 to 8 UNTIL i<n< li="" style="box-sizing: border-box;"></n<>

o STEP 6: SET j = 1 .REPEAT STEP 7 UNTIL j<=i

o STEP 7: PRINT * and SET j=j+1

o STEP 8: PRINT new line and SET i=i+1

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 2: SET i =1.REPEAT STEP 3 to 7UNTIL i<=10.

o STEP 3: SET j=1.REPEAT STEP 4 and 5UNTIL j<=10.

o STEP 4: if(i==0 or i==10 or j==0 or j==10) then PRINT 1 else PRINT ""

o STEP 5: SET j=j+1

o STEP 6: PRINT new line.

o STEP 7: SET i=i+1

o STEP 8: END

Program:

1. public class pattern


2. {
3. public static void main(String[] args) {
4. for (int i = 1; i <= 10; i++)
5. {
6. for (int j = 1; j <= 10; j++)
7. {
8. if (i==1 || i==10 || j==1 || j==10 )
9. System.out.print(" 1");
10. else
11. System.out.print(" ");
12. }
13. System.out.println();
14. }
15. }}
Output:

10. Java program to print the following pattern on the console

12345
1234
123
12
1

Algorithm:

o STEP 1: START

o STEP 2: DEFINE i, j.

o STEP 3: SET n=5.

o STEP 4: SET i=n. REPEAT STEP 5 to STEP 7 UNTIL i>0.

o STEP 5: SET j=1. REPEAT STEP 6 UNTIL j<=i.

o STEP 6: PRINT j and SET j=j+1.

o STEP 7: PRINT "" and SET i=i-1.

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

You might also like