Java Program for Basic Incrementing Diamond Pattern(Inverted)

Print Basic Incrementing Diamond Pattern

In this program we’re going to code basic incrementing diamond star pattern program 

Take a input from user and then store it in variable called as no take count=no-1 , then take a for loop start from i=1 to i<=no and then take a inner for loop start from j=1 to j<=i and then print count variable after that increment count variable . Then take another second loop start from i=no to i>=1 then decrement count variable as count– then take another for loop start from j=1 to j<=i then print count variable then print line changement statement

Java Program for Basic Incrementing Diamond Pattern(Inverted)

Algorithm:

  • Take the number of rows as input from the user and store it in any variable.(‘no‘ in this case) and take count=no-1
  • Run a loop ‘r’ number of times to iterate through each of the rows. From i=1 to i<=no. The loop should be structured as  for (int i = 1; i <= no; i++) and for int j =1 to j++ for (int i = 1; i <= no; i++) inside this for loop take another j for loop for (int j = 1; j <= i; j++) and print count System.out.print(count); and then print count++ and print System.out.println();
  • Take another for loop after main loop for (int i = no; i>=1 ; i–) take count– after that take another inner j for loop for (int j = 1; j <=i; j++) and print System.out.print(count);

Code in Java:

import java.util.Scanner;
public class Pattern1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter no");
		int no = sc.nextInt();
        int count = no-1;
        
        for (int i = 1; i <= no; i++) {
			for (int j = 1; j <= i; j++) 
				System.out.print(count);
			count++;
			System.out.println();
		}
        for (int i = no; i>=1 ; i--) {
        	count--;
			for (int j = 1; j <=i; j++) 
				System.out.print(count);
			System.out.println();
		}
     }
 }
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

One comment on “Java Program for Basic Incrementing Diamond Pattern(Inverted)”