











Java program to print number star half diamond pattern type 4
Print number star half diamond pattern type 4
Take input from user i.e number of lines required (N value) and starting ‘a’ value.Take two loops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’). i starts from 1 and j starts from 1.The program is divided into two sections for increment part (i.e first 4 lines) and decrement part (last 3 lines). Two separate i and j loops are written for each part. In first part i is from a to n and in the second part i is from (n-i) to a.Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values in each line. j loop is executed until it reaches i value.Print ‘i’ value until the j loop reaches i value.Print the final value ‘i’ of each line and go to next line.Repeat the loop until the condition is failed.


Algorithm
- Take input from user i.e number of lines required (N value) and starting ‘a’ value.
- Take two loops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’). i starts from 1 and j starts from 1.
- The program is divided into two sections for increment part (i.e first 4 lines) and decrement part (last 3 lines). Two separate i and j loops are written for each part. In first part i is from a to n and in the second part i is from (n-i) to a.
- Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values in each line. j loop is executed until it reaches i value.
- Print ‘i’ value until the j loop reaches i value.
- Print the final value ‘i’ of each line and go to next line.
- Repeat the loop until the condition is failed.
Code in Java
import java.io.*; class PrepInsta { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n, i, j, a; System.out.print("Enter starting value:"); a = Integer.parseInt(br.readLine()); System.out.print("Enter N value:"); n = Integer.parseInt(br.readLine()); for (i = a; i <= n; i++) { for (j = a; j < i; j++) { System.out.print(i + "*"); } System.out.println(i); } for (i = (n - 1); i >= a; i--) { for (j = 1; j < (i - 1); j++) { System.out.print(i + "*"); } System.out.println(i); } } } This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)
Login/Signup to comment