0% found this document useful (0 votes)
4 views8 pages

Java Lecture 6

The document provides Java programming examples for creating various patterns using loops, including star patterns, number patterns, and hollow shapes. It includes code snippets for upper and lower parts of patterns, as well as homework problems that challenge students to print specific shapes like a hollow butterfly and Pascal's triangle. The content is aimed at teaching programming concepts through practical exercises.

Uploaded by

Aditya Kamble
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)
4 views8 pages

Java Lecture 6

The document provides Java programming examples for creating various patterns using loops, including star patterns, number patterns, and hollow shapes. It includes code snippets for upper and lower parts of patterns, as well as homework problems that challenge students to print specific shapes like a hollow butterfly and Pascal's triangle. The content is aimed at teaching programming concepts through practical exercises.

Uploaded by

Aditya Kamble
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

Java - Introduction to Programming

Lecture 6

Patterns - Part 2

1.​

import [Link].*;

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++) {
[Link]("*");
}

int spaces = 2 * (n-i);


for(int j=1; j<=spaces; j++) {
[Link](" ");
}

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


[Link]("*");
}
[Link]();
}

Apna College
//lower part
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {
[Link]("*");
}

int spaces = 2 * (n-i);


for(int j=1; j<=spaces; j++) {
[Link](" ");
}

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


[Link]("*");
}
[Link]();
}
}
}

2.​

import [Link].*;

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++) {
[Link](" ");
}

//stars
for(int j=1; j<=n; j++) {
[Link]("*");
}
[Link]();
}
}
}

Apna College
3.​

import [Link].*;

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++) {
[Link](" ");
}

//numbers
for(int j=1; j<=i; j++) {
[Link](i+" ");
}
[Link]();
}
}
}

Apna College
4.​

import [Link].*;

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++) {
[Link](" ");
}

//first part
for(int j=i; j>=1; j--) {
[Link](j);
}

//second part
for(int j=2; j<=i; j++) {
[Link](j);
}
[Link]();
}
}
}

Apna College
5.​

import [Link].*;

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++) {
[Link](" ");
}
for(int j=1; j<=2*i-1; j++) {
[Link]("*");
}
[Link]();
}

//lower part
for(int i=n; i>=1; i--) {
//spaces
for(int j=1; j<=n-i; j++) {
[Link](" ");
}
for(int j=1; j<=2*i-1; j++) {
[Link]("*");
}
[Link]();
}
}
}

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