0% found this document useful (0 votes)
0 views7 pages

Java Lecture 6

The document contains Java programming examples focused on creating various patterns using loops, including stars, numbers, and spaces. It also includes homework problems that challenge students to print shapes like a hollow butterfly, rhombus, Pascal's triangle, and pyramids. Each code snippet demonstrates different pattern formations for educational purposes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views7 pages

Java Lecture 6

The document contains Java programming examples focused on creating various patterns using loops, including stars, numbers, and spaces. It also includes homework problems that challenge students to print shapes like a hollow butterfly, rhombus, Pascal's triangle, and pyramids. Each code snippet demonstrates different pattern formations for educational purposes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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();
}

//lower part
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {

Apna College
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;

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


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

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

3.

Apna College
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();
}
}
}

4.

Apna College
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();
}
}
}

5.

import java.util.*;

public class Solutions {


public static void main(String args[]) {

Apna College
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();
}
}
}

Homework Problems
1. Print a hollow Butterfly.

Apna College
2. Print a hollow Rhombus.

*****

* *

* *

* *

*****

3. Print Pascal’s Triangle.


1

11

121

1331

14641

4. Print half Pyramid.

12

123

1234

12345

5. Print an Inverted Half Pyramid.

11111

222

33

Apna College

You might also like