Tag: java pattern program

  • Java program to print Inverted Equilateral Star Triangle

    Java program to print Inverted Equilateral Star Triangle

    In this tutorial, we will write a program to print an inverted equilateral triangle in java. Before that, you may go through the following topic in java.

    It is a Java Program to Print Reverse Pyramid Star Pattern or you can say upside-down equilateral triangle pattern in java.

    The program takes the user input for the number of rows to be taken to print the reverse equilateral star pattern.

    Example:

    Input: number = 7   
    Output:
     *************  
     ***********   
      *********    
       *******     
        *****     
         ***       
          * 

    Java program to print Inverted Equilateral Star Triangle

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int num, i, j, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        num = sc.nextInt();
    
        for (i = num; i > 0; i--)
        {
          for (j = 0; j < num - i; j++)
          {
            System.out.print(" ");
          }
    
          for (k = 0; k < i; k++)
          {
            System.out.print("*" + " ");
          }
    
          System.out.print("\n");
        }
      }
    }

    Output:

    Inverted Equilateral Star Triangle

  • Java Program to Print Left Triangle Star Pattern

    Java Program to Print Left Triangle Star Pattern

    In this tutorial, we will write a program to print the left triangle star pattern in java. Before that, you may go through the following topic in java.


    Java Program to Print Left Triangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int k = 5, num;
        int i, j;
    
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        num = sc.nextInt();
    
        for (i = 0; i < num; i++)
        {
          for (j = 2 *(num - i); j >= 0; j--)
            System.out.print(" ");
    
          for (j = 0; j <= i; j++)
            System.out.print("* ");
    
          System.out.println();
        }
      }
    }

    Output:

    left triangle star pattern

  • Java Program to Print Right Triangle Star Pattern

    Java Program to Print Right Triangle Star Pattern

    In this tutorial, we will write a program to print the right triangle star pattern in java. Before that, you may go through the following topic in java.

    It also can be called mirrored right triangle number pattern in java or a half pyramid pattern in java. The program takes a user input for the number of rows and displays it accordingly.


    Java Program to Print Right Triangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int i, j, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1; j <= i; j++)
          {
            System.out.print("*");
          }
    
          System.out.println("");
        }
      }
    }

    Output:


  • Java Program to Print Hollow Rectangle Pattern of Stars

    Java Program to Print Hollow Rectangle Pattern of Stars

    In this tutorial, we will write a program to print hollow rectangle pattern in java. Before that, you may go through the following topic in java.

    The program takes a user input for the number of rows and columns for the hollow rectangle. Then using two for loops it will print the star rectangular pattern in java.

    Example:

    Inputs:
          Rows: 6
          Columns:10
    Output:
    **********
    *        *
    *        *
    *        *
    *        *
    **********

    Java Program to Print Rectangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, columns, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
        System.out.print("Enter the no. of columns: ");
        columns = sc.nextInt();
    
        System.out.print("Rectangular star Pattern: \n\n");
        for (i = 1; i <= rows; i++)
        {
          for (j = 1; j <= columns; j++)
          {
            if (i == 1 || i == rows || j == 1 || j == columns)
              System.out.print("*");
            else
              System.out.print(" ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Hollow rectangle star pattern in java

  • Java Program to Print Odd Number Pyramid

    Java Program to Print Odd Number Pyramid

    In this tutorial, we will learn how to print odd number patterns in java. Before that, you may go through the following topic in java.

    The program takes a user input for the number of rows and prints the number pattern of the pyramid for odd numbers in java.


    Java Program to Print of Odd Number Pyramid

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int i, j, k = 1, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1; j <= i; j++)
          {
            System.out.print(k + " ");
            k = k + 2;
          }
    
          System.out.print("\n");
        }
      }
    }

    Output:

    odd number pattern program in java

  • Java Program to print Even Odd number Pyramid

    Java Program to print Even Odd number Pyramid

    In this tutorial, we will write a program to print even and odd number pyramid in java. Before that, you may go through the following topic in java.

    Example:

    Input: 
           No. of rows:  7
    Output:
    * 
    1* 
    *2* 
    1*3* 
    *2*4* 
    1*3*5* 
    *2*4*6*

    Java Program to print Even Odd number Pyramid

    The program takes a user input for the number of rows with the help of a Scanner class in Java. Then iterating through for loop and inner for loop, it prints the even and odd number pattern in Java.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int i, j, k, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1, k = i; j <= i; j++, k--)
          {
            if (k % 2 == 0)
              System.out.print(j);
            else
              System.out.print("*");
    
          }
    
          System.out.print("\n");
        }
      }
    }

    Output:

    odd even pattern in java

  • Java Program to Print Pascal Triangle

    Java Program to Print Pascal Triangle

    In this tutorial, we will write a program to print pascal triangle in java. Before that, you should have knowledge on the following topic in Java.

    Pascal’s triangle is one of the important examples that is taught to the student. The number outside Pascal’s triangle is zero (0), which is not visible. It is a pattern where the triangle starts from 1 at the top and below is the addition of the upper level as you can see in the figure below. The addition continues until the required level is reached.

    pascal's triangle

    Java Program to Print Pascal Triangle

    //Print Pascal Triangle in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int n, i, j, k, num = 1;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        n = sc.nextInt();
    
        for (i = 0; i < n; i++)
        {
          for (k = n; k > i; k--)
          {
            System.out.print(" ");
          }
    
          num = 1;
          for (j = 0; j <= i; j++)
          {
            System.out.print(num + " ");
            num = num *(i - j) / (j + 1);
          }
    
          System.out.println();
        }
      }
    }

    Output:

    pascal triangle in java

  • Java Program to Print Diamond Pattern

    Java Program to Print Diamond Pattern

    In this tutorial, we will write a java program to print diamond pattern of stars. Before that, you should have knowledge on the following topic in Java.


    Java Program to Print Diamond Pattern

    Source Code: the program asks the user for the input of a number of rows for the diamond star pattern in java. Then using for loop, the diamond shape pattern is displayed on the screen.

    // Print Diamond star Pattern in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int n, j, i, space = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the no. of Rows: ");
        n = scan.nextInt();
    
        space = n - 1;
    
        for (i = 1; i <= n; i++)
        {
          for (j = 1; j <= space; j++)
          {
            System.out.print(" ");
          }
    
          space--;
          for (j = 1; j <= (2 *i - 1); j++)
          {
            System.out.print("*");
          }
    
          System.out.println();
        }
    
        space = 1;
    
        for (i = 1; i <= (n - 1); i++)
        {
          for (j = 1; j <= space; j++)
          {
            System.out.print(" ");
          }
    
          space++;
          for (j = 1; j <= (2 *(n - i) - 1); j++)
          {
            System.out.print("*");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    java diamond pattern of star

  • Java Program to Print Floyd’s Triangle

    Java Program to Print Floyd’s Triangle

    In this tutorial, we will write a java program to display Floyd’s Triangle. Before that, you should have knowledge on the following topic in Java.


    Java Program to Print Floyd’s Triangle

    The program asks the user for the number of rows for the Floyd triangle. Using inner and outer for loop it prints the pattern in java.

    //Print Floyd's Triangle in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int range, i, j, k = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        range = scan.nextInt();
    
        System.out.print("Floyd's Triangle:\n");
        for (i = 1; i <= range; i++)
        {
          for (j = 1; j <= i; j++, k++)
          {
            System.out.print(k + " ");
          }
    
          System.out.println();
    
        }
      }
    }

    Output:

    print Floyd’s Triangle in java

  • Java Program to Print Pyramid Pattern of Stars

    Java Program to Print Pyramid Pattern of Stars

    This article will show you how to print a simple pyramid pattern of stars in java.

    Patterns are one of the easiest ways to improve the coding skills for java. The frequent use of loops can increase your skills and printing pattern order.

    We will see two programs:

    1. To print a full pyramid by declaring the number of rows in a program.
    2. To print a full pyramid by taking user input for the number of rows.

    1. Java program to print the full pyramid pattern.

     //Pattern full pyramid
     
     public class Main
     { 
      public static void main(String[] args)  
      {  
        int rows = 6;  
        
       for (int i = 0; i < rows + 1; i++) 
       { 
        for (int j = rows; j > i; j--) 
        {  
           System.out.print(" ");  
        }  
         for (int k = 0; k < (2 * i - 1); k++) 
         {  
           System.out.print("*");  
         }  
          System.out.println();  
       }  
      }  
     }

    After the execution of the program, you will get the following result:


    2. Java program to print the full pyramid pattern (user input)

     import java.util.Scanner;
     
     public class Main
     { 
      public static void main(String[] args)  
      {  
         Scanner sc = new Scanner(System.in);
     
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt(); 
        
        for (int i = 0; i < rows + 1; i++) 
        { 
         for (int j = rows; j > i; j--) 
         {  
           System.out.print(" ");  
         }  
          for (int k = 0; k < (2 * i - 1); k++) 
          {  
            System.out.print("*");  
          }  
         System.out.println();  
       }  
      }  
     }

    After the execution of the program, you will get the following result: