Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Java Program for Diamond Star Pattern
May 24, 2020
Printing Diamond Star Pattern
In this program we’re going to code diamond star pattern program .
In this program we’re taking input from user and store in the variable names as n and then we will run a for loop start from j=n to j>i+1 and then print spaces after that take a j from loop again start from j=0 to j<=i*2 after that print star statement and then again take another for loop start from i=n to i>0 and then take a for loop star from k=n to k– and then print spaces after that take another loop to print star
Algorithm:
Enter the number input from the user and store it in any variable.(‘n‘ in this case).
Run a loop ‘n’ number of times to iterate through each of the rows. From i=0 to i<n. The loop should be structured as for( i=0 ; i
Inside main loop take another loop start from j=n to j– and then print System.out.println(“”);
After this inside main loop take another loop to print stars start from j=0 to j++ and then print star System.out.println(“*”)
After 1st mail loop take another 2nd main loop start from i=n to i–
Inside this take 1st inner loop start from k=n to k– and then print spaces System.out.print(” “);
After this take another inner loop to print stars start from j=0 to j++ System.out.print(“*”);
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 n=sc.nextInt();
for (int i=0; i<n; i++){
for (int j=n; j>i+1; j--)
System.out.print(" ");
for (int j=0; j<=i*2; j++ )
System.out.print("*");
System.out.println();
}
for(int i=n;i>0;i--)
{
for(int k=n;k>i;k--)
System.out.print(" ");
for(int j=0;j<i*2-1;j++)
System.out.print("*");
System.out.println();
}
}
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)
import java.util.Scanner;
public class Patterns{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number : “);
int row = sc.nextInt();
public class DiamondStarPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // n represents the number of rows for the upper half (excluding the middle line)
// Combined loop for upper and lower halves
for (int i = 0; i < 2 * n – 1; i++) {
int currentRow = i < n ? i : 2 * n – 2 – i; // Mirror row index for the lower half
// Printing spaces
for (int j = 0; j < n – currentRow – 1; j++) {
System.out.print(" ");
}
public class DiamondStarPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // n represents the number of rows for the upper half (excluding the middle line)
// Combined loop for upper and lower halves
for (int i = 0; i < 2 * n – 1; i++) {
int currentRow = i < n ? i : 2 * n – 2 – i; // Mirror row index for the lower half
// Printing spaces
for (int j = 0; j < n – currentRow – 1; j++) {
System.out.print(" ");
}
import java.util.Scanner;
public class Patterns{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number : “);
int row = sc.nextInt();
for(int i=1; i<=row; i++) {
for(int j=1; j<=row-i; j++)
System.out.print(" ");
for(int j=1; j=1; i–) {
for(int j=row-1; j>=i; j–)
System.out.print(” “);
for(int j=1; j<=2*i-1; j++)
System.out.print("*");
System.out.println();
}
}
}
package patternPrinting;
import java.util.Scanner;
public class DiamondStarPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // n represents the number of rows for the upper half (excluding the middle line)
// Combined loop for upper and lower halves
for (int i = 0; i < 2 * n – 1; i++) {
int currentRow = i < n ? i : 2 * n – 2 – i; // Mirror row index for the lower half
// Printing spaces
for (int j = 0; j < n – currentRow – 1; j++) {
System.out.print(" ");
}
// Printing stars
for (int j = 0; j < 2 * currentRow + 1; j++) {
System.out.print("*");
}
// Move to the next line
System.out.println();
}
}
}
import java.util.*;
class daimondpattern
{
public static void main(String ar[])
{
int i,j;
Scanner sc=new Scanner(System.in);
int n=sc.newInt();
int l;
for(i=-n;i<=n;i++)
{
if(i<0) {
l=-i;
}
else
{
l=i;
}
for(j=0;j<l;j++)
{
System.out.print(" ");
}
for(j=0;j<n-l+1;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n=5;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=5-i+1; j++)
{
System.out.print(" ");
}
for(int j=1; j<=(2*i-1); j++)
{
System.out.print("*");
}
System.out.println();
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print(" ");
}
for(int j=1; j<=(2*n+1)-(2*i); j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Kindly join our Discord channel for all the technical and course related queries.
import java.util.*;
public class Main{
public static void main(String[] args){
int n = new Scanner(System.in).nextInt();
int count = 1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++) System.out.print(" ");
for(int k=1;k=1;i–){
for(int j=1;j<=n-i;j++) System.out.print(" ");
for(int k=1;k<=count;k++) System.out.print("*");
count -= 2;
System.out.println();
}
}
}
import java.util.Scanner;
public class DiamondPattern5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter No of Rows : “);
int r = sc.nextInt();
for(int i =1;i<=r;i++){
if(i<=r/2+1) {
for(int j=1;j<=(r/2+1-i);j++) {
System.out.print(" ");
}
for(int k=1;k<=(2*i-1);k++) {
System.out.print("*");
}
}else {
for(int l = 1;l<=i-(r/2+1);l++) {
System.out.print(" ");
}
for(int m =1;m<=(2*r+1)-(2*i);m++) {
System.out.print("*");
}
}
System.out.println();
}
}
}
#java
public static void main(String[] args) {
int n = 5;
for(int i=1; i=i; j–){
System.out.print(” “);
}
for(int j=1; j<=i; j++){
System.out.print("* ");
}
for(int j=1; j<i; j++){
System.out.print("* ");
}
System.out.println();
}
for(int i=1; i<=n; i++){
for(int j=1; j=i; j–){
System.out.print(“* “);
}
for(int j=n-1; j>i; j–){
System.out.print(“* “);
}
System.out.println();
}
}
Thank you prepinsta for publishing my code…
package patternPrinting;
import java.util.Scanner;
public class DiamondStarPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // n represents the number of rows for the upper half (excluding the middle line)
// Combined loop for upper and lower halves
for (int i = 0; i < 2 * n – 1; i++) {
int currentRow = i < n ? i : 2 * n – 2 – i; // Mirror row index for the lower half
// Printing spaces
for (int j = 0; j < n – currentRow – 1; j++) {
System.out.print(" ");
}
// Printing stars
for (int j = 0; j < 2 * currentRow + 1; j++) {
System.out.print("*");
}
// Move to the next line
System.out.println();
}
}
}