Program to Print Kite Pattern in Java
In this tutorial, we are going to print kite pattern in Java. Creating a Java program that uses spaces and asterisks (*) to print a kite design. The size of the kite, or the length of the diamond's side, is represented by the integer n, which the program should receive as input.
Example:
Input: 3
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Explanation: We given input as 3 such that a kite pattern is a combination of three triangles lets say as, Upper section, middle section, lower section.
Approach: Using pattern printing
ALGORITHM:
Step 1: Take input of valid integer n.
Step 2: Make the upper part of size n.
Step 3: Make the lower part of size n.
Step 4: Finally, make the tail of kite(constant size).
Step 5: Print the output.
Filename: PatternPrinting.java
import java.io.*; public class PatternPrinting { public static void main(String[] args) { int a, b, c, spaceCount, initialSpace = 4; char symbol = '*'; for (a = 1; a <= 5; a++) { // For printing the space for (spaceCount = initialSpace; spaceCount >= 1; spaceCount--) { System.out.print(" "); } // Printing the * for (b = 1; b <= a; b++) { System.out.print(symbol); } for (c = 1; c <= (a - 1); c++) { if (a == 1) { continue; } System.out.print(symbol); } System.out.println(); initialSpace--; } initialSpace = 1; for (a = 4; a >= 1; a--) { for (spaceCount = initialSpace; spaceCount >= 1; spaceCount--) { System.out.print(" "); } for (b = 1; b <= a; b++) { System.out.print(symbol); } for (c = 1; c <= (a - 1); c++) { System.out.print(symbol); } initialSpace++; System.out.println(); } initialSpace = 3; for (a = 2; a <= 5; a++) { if ((a % 2) != 0) { for (spaceCount = initialSpace; spaceCount >= 1; spaceCount--) { System.out.print(" "); } for (b = 1; b <= a; b++) { System.out.print(symbol); } } if ((a % 2) != 0) { System.out.println(); initialSpace--; } } } }
Output:
* *** ***** ******* ********* ******* ***** *** * *** *****
Complexity analysis:
Time complexity: The time complexity of the program is O(n2).
Space complexity: The space complexity of the program is O(1).