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

Java Lecture 6

Uploaded by

vrukmangad74
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)
2 views8 pages

Java Lecture 6

Uploaded by

vrukmangad74
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