PatternsTriangular Pattern

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 31:- Triangular Pattern

Here are a few examples of triangular patterns using loops in Java:

  1. Left-aligned Triangle:

markdownCopy code

* ** *** **** *****

javaCopy code

public class LeftAlignedTriangle {    public static void main(String[] args) {        int n = 5;        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= i; j++) {                System.out.print("*");            }            System.out.println();        }    } }

  1. Right-aligned Triangle:

markdownCopy code

    *   **  *** **** *****

javaCopy code

public class RightAlignedTriangle {    public static void main(String[] args) {        int n = 5;        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= n - i; j++) {                System.out.print(" ");            }            for (int k = 1; k <= i; k++) {                System.out.print("*");            }            System.out.println();        }    } }

  1. Inverted Left-aligned Triangle:

markdownCopy code

***** **** *** ** *

javaCopy code

public class InvertedLeftAlignedTriangle {    public static void main(String[] args) {        int n = 5;        for (int i = 5; i >= 1; i--) {            for (int j = 1; j <= i; j++) {                System.out.print("*");            }            System.out.println();        }    } }

  1. Inverted Right-aligned Triangle:

markdownCopy code

***** ****  ***   **    *

javaCopy code

public class InvertedRightAlignedTriangle {    public static void main(String[] args) {        int n = 5;        for (int i = 1; i <= n; i++) {            for (int j = 1; j < i; j++) {                System.out.print(" ");            }            for (int k = i; k <= n; k++) {                System.out.print("*");            }            System.out.println();        }    } }

You can adjust the value of n to control the size of the triangles. Experiment with different loop structures to create other interesting patterns as well.

6. Loops

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?