0% found this document useful (0 votes)
59 views

Java - Introduction To Programming: Patterns - Part 2

The document discusses Java code examples for printing different patterns. It includes 5 code examples that print patterns such as a diamond shape, numbers triangle, half pyramid, and inverted half pyramid using for loops and conditional printing of characters like asterisks and spaces. The document concludes with 4 homework problems on printing a hollow butterfly, hollow rhombus, Pascal's triangle, and half pyramid patterns.

Uploaded by

Abhinav Ashish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Java - Introduction To Programming: Patterns - Part 2

The document discusses Java code examples for printing different patterns. It includes 5 code examples that print patterns such as a diamond shape, numbers triangle, half pyramid, and inverted half pyramid using for loops and conditional printing of characters like asterisks and spaces. The document concludes with 4 homework problems on printing a hollow butterfly, hollow rhombus, Pascal's triangle, and half pyramid patterns.

Uploaded by

Abhinav Ashish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java - Introduction to Programming

Lecture 6

Patterns - Part 2

1.

import java.util.*;

public class Solutions {


public static void main(String args[]) {
int n = 4;

//upper part
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
System.out.print("*");
}

int spaces = 2 * (n-i);


for(int j=1; j<=spaces; j++) {
System.out.print(" ");
}

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


System.out.print("*");
}
System.out.println();
}

Apna College
//lower part
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {
System.out.print("*");
}

int spaces = 2 * (n-i);


for(int j=1; j<=spaces; j++) {
System.out.print(" ");
}

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


System.out.print("*");
}
System.out.println();
}
}
}

2.

import java.util.*;

public class Solutions {


public static void main(String args[]) {
int n = 5;

Apna College
for(int i=1; i<=n; i++) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}

//stars
for(int j=1; j<=n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Apna College
3.

import java.util.*;

public class Solutions {


public static void main(String args[]) {
int n = 5;

for(int i=1; i<=n; i++) {


//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}

//numbers
for(int j=1; j<=i; j++) {
System.out.print(i+" ");
}
System.out.println();
}
}
}

Apna College
4.

import java.util.*;

public class Solutions {


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

//first part
for(int j=i; j>=1; j--) {
System.out.print(j);
}

//second part
for(int j=2; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

Apna College
5.

import java.util.*;

public class Solutions {


public static void main(String args[]) {
int n = 5;

//upper part
for(int i=1; i<=n; i++) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
for(int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}

//lower part
for(int i=n; i>=1; i--) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
for(int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Apna College
Homework Problems
1. Print a hollow Butterfly.

2. Print a hollow Rhombus.

*****

* *

* *

* *

*****

3. Print Pascal’s Triangle.

11

121

1331

14641

4. Print half Pyramid.

Apna College
12

123

1234

12345

5. Print Inverted Half Pyramid.

11111

222

33

Apna College

You might also like