Tricky Star Hyphen Combination Pattern Program in Java

Below Java concepts are used to print that patterns

  • For Loop
  • While Loop
  • if..else

1). Print Left Half Diamond Pattern using Star(*) and Hyphen(-) in Java

star hyphen combination pattern program in c
import java.util.*;  
class Main{
    public static void main(String ...a){
    int i,j,k;
    Scanner sc= new Scanner(System.in);
    System.out.print("enter the value n: ");  
    int n= sc.nextInt();  
    for(i=0;i<n;i++)
    {
	    for(j=0;j<n-i;j++){
            System.out.print(" ");
	    }
		if(i%2==0){
	        for(k=0;k<=i;k++){
		        System.out.print("*");
	        }
        }else{
            for(k=0;k<=i;k++){
                System.out.print("-");
            }
        }
		System.out.println("");
	}
	for(i=n-1;i>0;i--)
    {
	    for(j=n;j>=i;j--){
            System.out.print(" ");
	    }
	    if(i%2==0){
	        for(k=i;k>0;k--){
		        System.out.print("-");
	        }
	    }else{
	        for(k=i;k>0;k--){
                System.out.print("*");
            }
        }
		System.out.println("");
	}
    }
} 

Output

enter the value n: 5
     *
    --
   ***
  ----
 *****
  ----
   ***
    --
     *

2). Print Right Half Diamond Pattern using Star(*) and Hyphen(-) in Java

star hyphen combination pattern program in c
import java.util.*;  
class Main{
    public static void main(String ...a){
    int i,j,k;
    Scanner sc= new Scanner(System.in);
    System.out.print("enter the value n: ");  
    int n= sc.nextInt();  
    for(i=1;i<=n;i++)
    {
        if(i%2==0){
	        for(k=1;k<=i;k++){
		        System.out.print("-");
	        }
        }else{
            for(k=1;k<=i;k++){
                System.out.print("*");
            }
        }
		System.out.println("");
	}
for(i=n;i>1;i--)
    {
        if(i%2==0){
	        for(k=i;k>1;k--){
            System.out.print("*");
	        }
        }
        else{
            for(k=i;k>1;k--){
		        System.out.print("-");
	        }
        }
		System.out.println("");
	}
    }
} 

Output

enter the value n: 5
*
--
***
----
*****
----
***
--
*

3). Program to print Triangle pattern using Star(*) and Hyphen(-) in Java

star hyphen combination 3 pattern program in C
import java.util.*;  
class Main{
    public static void main(String ...a){
    int i,j,k;
    Scanner sc= new Scanner(System.in);
    System.out.print("enter the value n: ");  
    int n= sc.nextInt();  
    for(i=0;i<n;i++){
	    for(k=1;k<n-i;k++){
            System.out.print(" ");
	    }
	    System.out.print("*");
        for(j=0;j<=i-1;j++){
            System.out.print("-");
        }
	    for(j=1;j<i;j++){
	        System.out.print("-");
	    }
	    if(i>0){
            System.out.print("*");
	    }
        System.out.println("");
    }
    }
} 

Output

enter the value n: 5
    *
   *-*
  *---*
 *-----*
*-------*

4). Program to Print Full Diamond Pattern using Star(*) and Hyphen(-) in Java

star hyphen combination full pattern program in c
import java.util.*;  
class Main{
    public static void main(String ...a){
    int i,j,k;
    Scanner sc= new Scanner(System.in);
    System.out.print("enter the value n: ");  
    int n= sc.nextInt();  
    for(i=1;i<=n;i++)
    {
	    for(j=1;j<=n-i;j++){
		    System.out.print(" ");
	    }
		if(i%2==1){
	        for(k=1;k<=2*i-1;k++){
                System.out.print("*");
	        }
        }else{
            for(k=1;k<=2*i-1;k++){
                System.out.print("-");
            }
        }
		System.out.println("");
	}
    for(i=n;i>=1;i--)
    {
	    for(j=n;j>=i;j--){
            System.out.print(" ");
	    }
	    if(i%2==1){
	        for(k=2*i-2;k>1;k--){
		        System.out.print("-");
	        }
	    }else{
	        for(k=2*i-2;k>1;k--){
		        System.out.print("*");
	        }
	    }
	    System.out.println("");
        }
    }
} 

Output

enter the value n: 5
    *
   ---
  *****
 -------
*********
 -------
  *****
   ---
    *
     

Leave a Comment