Java Program for Printing Basic incrementing Diamond Pattern(Inverted Sandwich)

Printing Basic Incrementing Diamond Pattern (Inverted Sandwich)

In this program we’re going to code pyramid star pattern program .

The logic of this program is this take any number input from user and store it in variable n and then run the for loop start from 0 to i

start from j=n-i to j– and again take another for loop start from j=0 to j++ to print stars and after this take line changement statement after the end of main for loop

Java Program for Basic incrementing Diamond Pattern(Inverted Sandwich)

Algorithm:

  1. Take the number of rows as input from the user and store it in any variable.(‘no‘ in this case). and take int count = no-2;
  2. Run a loop ‘no’ 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++) after this take another loop j for (int j = 1; j <= i; j++)  and take System.out.print(count); and take count++; take this to change line System.out.println(); 
  3. Take another loop i for (int i = no; i>=1 ; i–) inside this take count– and take another for loop to print count variable for (int j = 1; j <=i; j++) and after this main for loop System.out.println();

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-2;
        
        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)

3 comments on “Java Program for Printing Basic incrementing Diamond Pattern(Inverted Sandwich)”


  • Pulivendula

    import java.util.*;
    public class Main
    {
    public static void main(String[] args)
    {
    int n=4;
    int k=2;
    for(int i=1; i<=n; i++)
    {
    for(int j=1; j<=i; j++)
    {
    System.out.print(k);
    }
    k++;
    System.out.println();
    }

    int k1=5;
    for(int i=1; i<=n; i++)
    {
    for(int j=1; j<=n-i+1; j++)
    {
    System.out.print(k1);
    }
    k1–;
    System.out.println();
    }

    }
    }


    • 2100049006ece

      import java.util.*;
      public class pattern{
      public static void main(String[] args){
      Scanner sc=new Scanner(System.in);
      System.out.print(“enter number of rows=”);
      int n=sc.nextInt();
      int k=1;
      for(int i=0;i<n;i++){
      k++;
      for(int j=0;j=1;i–){
      k–;
      for(int j=0;j<i;j++){
      System.out.print(k);
      }
      System.out.println();
      }
      }
      }