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.






Quick Enquiry

Please wait while submission in progress...


Contact Us

Chennai

  93450 45466

Bangalore

 93450 45466

Coimbatore

 95978 88270

Online

93450 45466

Madurai

97900 94102

Pondicherry

93635 21112

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office
& Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

Coimbatore

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

Other Locations

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

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

    Adyar, Adambakkam, Anna Salai, Ambattur, Ashok Nagar, Aminjikarai, Anna Nagar, Besant Nagar, Chromepet, Choolaimedu, Guindy, Egmore, K.K. Nagar, Kodambakkam, Koyambedu, Ekkattuthangal, Kilpauk, Meenambakkam, Medavakkam, Nandanam, Nungambakkam, Madipakkam, Teynampet, Nanganallur, Navalur, Mylapore, Pallavaram, Purasaiwakkam, OMR, Porur, Pallikaranai, Poonamallee, Perambur, Saidapet, Siruseri, St.Thomas Mount, Perungudi, T.Nagar, Sholinganallur, Triplicane, Thoraipakkam, Tambaram, Vadapalani, Valasaravakkam, Villivakkam, Thiruvanmiyur, West Mambalam, Velachery and Virugambakkam.

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur 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!