0% found this document useful (0 votes)
2 views11 pages

Star Pattern Programming Examples

The document contains Java code examples for various star pattern programming exercises. It includes classes for creating different types of triangles (ascending, descending, equilateral) and patterns like butterflies, with detailed comments on the logic used for each pattern. Each example demonstrates the use of nested loops to control the number of stars and spaces printed in the console.

Uploaded by

pnikambe2066
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Star Pattern Programming Examples

The document contains Java code examples for various star pattern programming exercises. It includes classes for creating different types of triangles (ascending, descending, equilateral) and patterns like butterflies, with detailed comments on the logic used for each pattern. Each example demonstrates the use of nested loops to control the number of stars and spaces printed in the console.

Uploaded by

pnikambe2066
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pattern Programming Examples

package StarLogic;
public class Triangle_Left_Ascending
{
public static void main(String[] args)
{
// *
// **
// ***
// ****
// *****
//Step 1: Count Rows = 05 Count Columns = 05
//
int star = 1; // Write No of star in first Row
for(int i=1; i<=5; i++) // Outer for loop used for Rows
{
for(int j=1; j<=star; j++) // Inner Loop for Columns
{
[Link]("*");
}
[Link](); // after printing star go on next line
star++; // Increment in stars
}
}
}

-----------------------------------------------------------------------
package StarLogic;
public class Triangle_Left_Descending
{
public static void main(String[] args)
{
//*****
//****
//***
//**
//*
//Step1 : Count Rows = 5; Count Columns = 5
int star = 5; // No of star present in first row
for(int i=1; i<=5; i++) // Outer loop for rows
{
for(int j=1; j<=star; j++) // inner loop for
{
[Link]("*");
}
[Link]();
star--;
}
}
}
package StarLogic;
public class E6_Triangle_Right_Descending {
public static void main(String[] args) {
//*****
// ****
// ***
// **
// *
int space = 0; // On the First Row there is no space, space = 0;
int star = 5; // Outer Loop for the No of Rows = 5
for (int i=1; i<=5; i++)
{
for (int j=1; j<=space; j++ )//1st consider inner for loop for the Space
{
[Link](" ");// Use Single Space and on Print for Inner For Loop
}
for (int j=1; j<=star; j++)
{
[Link]("*");
}
[Link]();
space++;
star--;

}
}
}
----------------------------------------------------------------------------------
package StarLogic;
public class E7_Triangle_Right_Acending {
public static void main(String[] args) {
// *
// **
// ***
// ****
//*****
int space = 4; // Space in first row, space = 4
int star = 1; // star on first row, star = 1
for(int i=1; i<=5; i++) // Outer loop for No of Rows = 5
{
for(int j=1; j<= space; j++) // 1st inner loop : Space
{
[Link](" "); // Don't Use Println()
}
for(int j=1; j<= star; j++)
{
[Link]("*");
}
[Link]();
space--;
star++;
}
}
}-----------------------------------------------------------------------------
package StarLogic;
public class E8_Equilateral_Downward{
public static void main(String[] args){
//*******
// *****
// ***
// *
int space=0;
int star=7;
for(int i=1; i<=4; i++) // Outer For Loop; Rows= 4
{
for(int j=1; j<=space; j++) // Inner for Loop
{
[Link](" ");
}
for(int j=1; j<=star; j++) // Inner for Loop
{
[Link]("*");
}
[Link]();
space++; // Increment of Space by 1
star = star-2; // Decrement of Star by 2
}
}
}-------------------------------------------------------------------------------
package StarLogic;

public class E9_EquilateralTriagle_Upward


{ public static void main(String[] args)
{
// *
// ***
// *****
//*******

int space=3;
int star=1;
for(int i=1; i<=4; i++) // Outer For loop; Rows=4;
{
for(int j=1; j<=space; j++)
{
[Link](" ");
}
for(int j=1; j<=star; j++) // Inner For loop; Column;
{
[Link]("*");
}
[Link]();
space--;
star=star+2;
}
}
}----------------------------------------------------------------------------
package StarLogic;
public class E10_EquilateralTriangle_Upward_WithSpacing
{ public static void main(String[] args) {
// Equilateral Triangle Upward With Space
// *
// * *
// * * *
//* * * *
int space=3;
int star=1;
for(int i=1; i<=4; i++) // Outer for loop for the no of Rows
{
for(int j=1; j<=space; j++) // Inner For Loop for Space
{
[Link](" ");
}
for(int j=1; j<=star; j++) // Inner For Loop for star
{
[Link]("* ");
}
[Link]();
space--;
star++;
}
}
}----------------------------------------------------------------------------------
package StarLogic;
public class E11_EquilateralTriangle_Downward_WithSpacing
{ public static void main(String[] args)
{ // Equilateral Triangle Downward With Space
//* * * * *
// * * * *
// * * *
// * *
// *
int space = 0; // No of Space in First Row
int star = 5; // No of Starts in First Row
for(int a=1; a<=5; a++) // Outer For Loop
{
for(int b=1; b<=space; b++) //Inner For Loop
{
[Link](" ");
}
for(int c=1; c<=star; c++) //Inner For Loop
{
[Link]("* ");
}
[Link]();
space++;
star--;
}
}
}------------------------------------------------------------------------------
package StarLogic;
public class F12_DescAsc_DecreasingIncreasing {
public static void main(String[] args) {
//----------------------------------------
//*****
//****
//***
//**
//*
//**
//***
//****
//*****
//--------------------------------------

//Step .1 Count Total no of Rows = 9; Use Outer for loop for No. of Rows

int star = 5; //Count Total no of star in first Row


for(int i=1; i<=9; i++) // Outer for loop used for No. of Rows
{
for(int j=1; j<=star; j++) // Inner for loop for Col
{
[Link]("*");
}
[Link]();
//star--;
if(i<5) // OR (i<=4) // For Descending star
{
star--;
}
else // For Ascending star
{
star++;
}
}
}
}
package StarLogic;
public class F13_AscDesc_IncreasingDescreasing {
public static void main(String[] args) {
//*
//**
//***
//****
//***
//**
//*
int star = 1; // Total No of Star in First/Initial Row
for(int i=1; i<=7; i++) // Outer for loop for Total No of Rows=7
{
for(int j=1; j<=star; j++) // Inner for loop for the column
{
[Link]("*");
}
[Link]();
//star++;
if(i<4) // if(i<=3) // for Increasing star; star++
{
star++;
}
else // for Decreasing star; star--
{
star--;
}
}
}
}
package StarLogic;
public class F14_ButterFly {
public static void main(String[] args) {
//* *
//** **
//*** ***
//**** ****
//***** *****
//**** ****
//*** ***
//** **
//* * ( Remember : Star Space Space Star)

int star = 1; // Star on the first Row Left


int space = 5; // Space on the first Row Left
int star2 = 1; // Star on the first Row Right
int space2 = 4; // Space on the first Row Right

for(int i=1; i<=9; i++) //Outer for loop used for No. of Rows = 9
{
for(int j=1; j<=star; j++) // Inner For loop for Star(Left)
{
[Link]("*");
}
for(int k=1;k<=space; k++) // Inner For loop for space(Left)
{
[Link](" ");
}
for(int m=1; m<=space2; m++)
{
[Link](" ");
}
for(int l=1; l<=star2;l++)
{
[Link]("*"); // Inner For loop for Star(Right)
}

if(i<5)
{
star++;
space--;
star2++;
space2--;
}
else
{
star--;
space++;
star2--;
space2++;
}
[Link]();
}
}}----------------------------------------------------------------------

You might also like