For Enquiry: 93450 45466

JAVA Pattern Programs


Pattern Programs in JAVA can enhance your programming and logical skills using loops. Here we will learn some of these patterns under 3 categories:
Star Pattern Programs with Java
Numeric Pattern Programs with Java
Character Pattern Programs with Java
So let’s start with the star pattern programs

Star Pattern Programs In With Java

Java program for printing Right Half pyramid or Right Triangle

import java.util.Scanner;
public class RightHalfPyramid{
public static void main(String[] args) {
System.out.println("Enter the number of rows for pattern: ");
Scanner sc = new Scanner(System.in);  // taking the input with scanner
int rows = sc.nextInt();
for(int row = 1; row <= rows; ++row) {
for(int col = 1; col <= row; ++col) {
System.out.print("* ");
}
System.out.println();
sc.close();
}
}
Output for the above program

Enter the number of rows for pattern: 5
* 
* * 
* * * 
* * * * 
* * * * *
Java program for printing Left Half pyramid or Left Triangle

import java.util.Scanner;
public class LeftHalfPyramid
{
public static void main(String[] args)
{
System.out.println("Enter the number of rows for pattern: ");
Scanner sc = new Scanner(System.in);  // taking the input with scanner
int rows = sc.nextInt();
for (int row=1; row<=rows; row++)
{
// Printing space in decreasing order
for (int col=rows; col>row; col--)
{
System.out.print(" ");
}
// Printing star in increasing order
for (int star=1; star<=row; star++)
{
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}
Output for the above program

Enter the number of rows for pattern: 5
    *
   **
  ***
 ****
*****
Java program for printing Full Pyramid or Triangle

import java.util.Scanner;

 

public class FullPyramid

{

public static void main(String[] args)

{

 

System.out.println("Enter the number of rows for pattern: ");

Scanner sc = new Scanner(System.in);  // taking the input with scanner

int rows = sc.nextInt();

 

for (int row=1; row<=rows; row++)

{

// Print space in decreasing order

for (int col=rows; col>row; col--)

{

System.out.print(" ");

}

// Print star in increasing order

for (int star=1; star<=row; star++)

{

System.out.print("* ");

}

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 5

    * 

   * * 

  * * * 

 * * * * 

* * * * *
Java program for printing Rightly Inverted Half Pyramid or Downward Right Half Triangle

            import java.util.Scanner;
            ย 
            public class InvertedHalfPyramid {
            ย 
            public static void main(String[] args) {
            ย 
            System.out.println("Enter the number of rows for pattern: ");
            Scanner sc = new Scanner(System.in);  // taking the input with scanner
            int rows = sc.nextInt();
            ย 
            for(int row = rows; row >= 1; --row) {
            for(int col = 1; col <= row; ++col) {
            System.out.print(" * ");
            }
            System.out.println();
            }
            sc.close();
            }
            }
            
Output for the above program

Enter the number of rows for pattern: 5 

 *  *  *  *  * 

 *  *  *  * 

 *  *  * 

 *  * 

 *
Java program for printing Left Inverted Half Pyramid or Downward Left Half Triangle

import java.util.Scanner;

public class InvertedHalfPyramid

{

 

public static void main(String[] args)

{

System.out.println("Enter the number of rows for pattern: ");

Scanner sc = new Scanner(System.in);  // taking the input with scanner

int rows = sc.nextInt();

for (int row= rows; row>= 1; row--)

{

for (int col=rows; col>row;col--)

{

System.out.print(" ");

}

for (int count=1;count<=row;count++)

{

System.out.print("*");

}

System.out.println("");

}

sc.close();

 

}

}

Check out this Complete Online Java Course by FITA. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real time applications using Servlets, Hibernate Framework, and Spring with Aspect-Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell, to make you an industry required certified java developer.

Output for the above program


Enter the number of rows for pattern: 5

*****

 ****

  ***

   **

    *
Java program for printing Inverted full pyramid or inverted triangle

import java.util.Scanner;

public class InvertedFullPyramid

{

 

public static void main(String[] args)

{

System.out.println("Enter the number of rows for pattern: ");

Scanner sc = new Scanner(System.in);  // taking the input with scanner

int rows = sc.nextInt();

for (int row= rows; row>= 1; row--)

{

for (int col=rows; col>row;col--)

{

System.out.print(" ");

}

for (int count=1;count<=row;count++)

{

System.out.print(" *");

}

System.out.println("");

}

sc.close();

 

}

}
Output for the above program

Enter the number of rows for pattern: 5

 * * * * *

  * * * *

   * * *

    * *

     *
Java Program for printing Diamond Pattern with stars

import java.util.Scanner;

 

public class Diamond

{

public static void main(String[] args)

{

// Scanner object

Scanner scanner = new Scanner(System.in);

 

// taking the number of rows with the scanner

System.out.println("Enter the number of rows for pattern: ");

 

int rows = scanner.nextInt();

 

for (int row=1; row<=rows; row++)

{

// Print space in decreasing order

for (int col=rows; col>row; col--)

{

System.out.print(" ");

}

// Print star in increasing order

for (int count=1; count<=(row * 2) -1; count++)

{

System.out.print("*");

}

System.out.println();

}

for (int row=rows-1; row>=1; row--)

{

// Printing space in increasing order

for (int col=rows-1; col>=row; col--)

{

System.out.print(" ");

}

// Printing stars in decreasing order

for (int count=1; count<=(row * 2) -1; count++)

{

System.out.print("*");

}

 

System.out.println();

}

scanner.close();

}

}
Output for the above program

Enter the number of rows for pattern: 5

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

     *
Java Program for printing Sand Glass with Stars

import java.util.Scanner;

 

public class SandGlass {

public static void main(String[] args) {

// scanner object

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows for pattern: ");

// taking the number of rows with scanner

int rows = sc.nextInt();

for (int row = 0; row <= rows - 1; row++) {

for (int col = 0; col < row; col++) {

System.out.print(" ");

}

for (int count = row; count <= rows - 1; count++) {

System.out.print("* ");

}

System.out.println("");

}

for (int row = rows - 1; row >= 0; row--) {

for (int col = 0; col < row; col++) {

System.out.print(" ");

}

for (int count = row; count <= rows - 1; count++) {

System.out.print("* ");

}

System.out.println("");

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 5

* * * * * 

 * * * * 

  * * * 

   * * 

    * 

    * 

   * * 

  * * * 

 * * * * 

* * * * * 

Check out this Complete Online Java Training by FITA. FITA provides a complete core and advanced Java course where you will be building real-time applications using J2EE, Servlets, Hibernate Framework, and Spring with Aspect-Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell, to make you an industry-certified java developer.

After the star pattern programs, we will now see the numeric pattern programs in java

Numeric Pattern Programs With Java

Java program to print simple numbered triangle pattern

import java.util.Scanner;

 

public class Main

{

public static void main(String[] args)

{

// scanner object

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows for pattern: ");

int n = sc.nextInt(); // taking input with scanner sc

int row, col,number;

 

for(row=0; row<n; row++) // outer loop for rows

{

number=1;

for(col=0; col<=row; col++) // inner loop for rows

{

// adding a space after number

System.out.print(number+ " ");

 

//incrementing value of number

number++;

}

// printing an empty line

System.out.println();

}

}

 

}
Output for the above program

Enter the number of rows for pattern: 

5

1 

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5
Java Program to print Floydโ€™s triangle pattern

import java.io.*;

import java.util.Scanner;

 

public class NumberPattern {

public static void printNums(int num) {

// initialising starting number

int row, col, number = 1;

 

for (row = 0; row < num; row++) {

for (col = 0; col <= row; col++) {

// // adding a space after number

System.out.print(number + " ");

 

// incrementing number at every column

number = number + 1;

}

 

// ending line after each row

System.out.println();

}

}

 

public static void main(String args[]) {

// scanner object

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows for pattern: ");

int num = sc.nextInt(); // taking input with scanner sc

printNums(num);

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 6

1 

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15 

16 17 18 19 20 21
Java Program for printing Pascalโ€™s triangle Pattern

import java.io.*;

import java.util.Scanner;

public class PascalPattern {

 

public static void main(String[] args) {

//scanner object

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows pattern: ");

// taking input with scanner

int rows = sc.nextInt();

int number = 1;

 

for(int row = 0; row < rows; row++) {

for(int space = 1; space < rows - row; ++space) {

System.out.print("  ");

}

 

for(int col = 0; col <= row; col++) {

if (col == 0 || row == 0)

number = 1;

else

number = number * (row - col + 1) / col;

 

System.out.printf("%4d", number);

}

// printing new line after iterations

System.out.println();

}

}

}
Output for the above program

Enter the number of rows for pattern: 6

             1

           1   1

         1   2   1

       1   3   3   1

     1   4   6   4   1

   1   5  10  10   5   1
Java Program for printing repeated numbers pattern

import java.util.Scanner;

 

public class RepeatedPattern

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in); //Taking rows value from the user

System.out.println("Enter the number of rows for pattern: ");

 

int rows = sc.nextInt();

for (int i = 1; i <= rows; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(i+" ");

}

 

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 6

1 

2 2 

3 3 3 

4 4 4 4 

5 5 5 5 5 

6 6 6 6 6 6
Java Program to Print Inverted Half Pyramid with numbers

import java.io.*;

import java.util.Scanner;

 

public class HalfPyramid {

 

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); // Scanner Object

System.out.println("Enter the number of rows for pattern: ");

// Taking rows value from the user with scanner

int rows = sc.nextInt();

 

for(int row = rows; row >= 1; --row) {

for(int col = 1; col <= row; ++col) {

System.out.print(col + " ");

}

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 6

 

1 2 3 4 5 6 

1 2 3 4 5 

1 2 3 4 

1 2 3 

1 2 

1
Java Program to print repeated number pattern

import java.io.*;

import java.util.Scanner;

 

public class MainClass

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);          // Scanner Object

System.out.println("Enter the number of rows for pattern: ");

int rows = sc.nextInt();         // Taking rows value from the user with scanner

 

for (int row = 1; row <= rows; row++)

{

 

for (int col = 1; col <= row; col++)

{

System.out.print(col+" ");

}

 

for (int col = row-1; col >= 1; col--)

{

System.out.print(col+" ");

}

 

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 6

1 

1 2 1 

1 2 3 2 1 

1 2 3 4 3 2 1 

1 2 3 4 5 4 3 2 1 

1 2 3 4 5 6 5 4 3 2 1
After numeric pattern programs, we have character pattern programs in java.

Character Pattern Programs With Java

Java program to print right triangle character pattern

import java.io.*;

import java.util.Scanner;

 

public class RightAlphaTri

{

public static void main(String[] args)

{

int alphabet = 65;     //ASCII value of A is 65

Scanner sc = new Scanner(System.in);  // Scanner Object

 

System.out.println("Enter the number of rows for pattern: ");

// Taking rows value from the user with a scanner

 

int rows = sc.nextInt();

for (int row = 0; row <= rows; row++)

{

for (int col = 0; col <= row; col++)

{

System.out.print((char) (alphabet + col) + " ");

}

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 6 

A 

A B 

A B C 

A B C D 

A B C D E 

A B C D E F 

A B C D E F G
Java program to print repeated character pattern

import java.io.*;

import java.util.Scanner;

 

public class Edureka

{

public static void main(String[] args)

{

int alphabet = 65;

Scanner sc = new Scanner(System.in);  // Scanner Object

System.out.println("Enter the number of rows for pattern: ");

 

// Taking rows value from the user with a scanner

 

int rows = sc.nextInt();

for (int row = 0; row<= rows; row++)

{

for (int col = 0; col <= row; col++)

{

System.out.print((char) alphabet + " ");

}

alphabet++;

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern: 6 

A 

B B 

C C C 

D D D D 

E E E E E 

F F F F F F
Java Program to Print Pyramid Pattern with alphabets

import java.io.*;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);  // Scanner Object

System.out.println("Enter the number of rows for pattern: ");

// Taking rows value from the user with scanner

int rows = sc.nextInt();

int alphabet = 65;

for (int row = 0; row <= rows; row++) {

for (int col = 8; col > row; col--) {

System.out.print(" ");

}

for (int count = 0; count <= row; count++) {

System.out.print((char) (alphabet + count) + " ");

}

System.out.println();

}

sc.close();

}

}
Output for the above program

Enter the number of rows for pattern:  6

        A 

       A B 

      A B C 

     A B C D 

    A B C D E 

   A B C D E F 

  A B C D E F G 

This was all printing patterns using java. To get in-depth knowledge of core Java and advanced java, J2EE SOA training along with its various applications and real-time projects using Servlets, Spring with Aspect-Oriented Programming (AOP) architecture, Hibernate Framework, and Struts through JDBC you can enroll in Certified Java Training in Chennai or Certified Java Training in Bangalore by FITA or a virtual class for this course, at an affordable price, bundled with real-time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.

FITAโ€™s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.





  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

  • Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!