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

Java Lecture 6

This document is a Java lecture focused on programming patterns, providing multiple code examples for generating various patterns such as stars, numbers, and shapes. It includes homework problems for students to practice creating a hollow butterfly, rhombus, Pascal's triangle, and pyramids. The lecture emphasizes understanding loops and formatting output in Java.

Uploaded by

anmol895711
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)
3 views7 pages

Java Lecture 6

This document is a Java lecture focused on programming patterns, providing multiple code examples for generating various patterns such as stars, numbers, and shapes. It includes homework problems for students to practice creating a hollow butterfly, rhombus, Pascal's triangle, and pyramids. The lecture emphasizes understanding loops and formatting output in Java.

Uploaded by

anmol895711
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 Lecture 6

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

//lower part
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {
[Link]("*");
}
int spaces = 2 * (n-i);
Java Lecture
for(int j=1; 6
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;

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]();
}
}
}
Java Lecture 6

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]();
}
}
}
Java Lecture 6

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]();
}
}
}
Java Lecture 6

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

Homework Problems
Java Lecture 6

1. Print a hollow Butterfly.

2. Print a hollow Rhombus.

*****
* *
* *
* *
*****
3. Print Pascal’s Triangle.

1
11
121
1331
14641
4. Print half Pyramid.

1
12
123
1234
12345
5. Print Inverted Half Pyramid.
11111
Java Lecture 6
222
33
4

Apna College

You might also like