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

Java Lecture 6

The document discusses Java code examples for printing different patterns. It provides 5 code examples that print patterns such as a hollow diamond, hollow rhombus, Pascal's triangle, half pyramid, and inverted half pyramid. The code uses for loops and conditionals to control the printing of characters or numbers in the desired pattern shapes. Homework problems are also listed that involve printing a hollow butterfly, hollow rhombus, Pascal's triangle, half pyramid, and inverted half pyramid.

Uploaded by

Somit Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Java Lecture 6

The document discusses Java code examples for printing different patterns. It provides 5 code examples that print patterns such as a hollow diamond, hollow rhombus, Pascal's triangle, half pyramid, and inverted half pyramid. The code uses for loops and conditionals to control the printing of characters or numbers in the desired pattern shapes. Homework problems are also listed that involve printing a hollow butterfly, hollow rhombus, Pascal's triangle, half pyramid, and inverted half pyramid.

Uploaded by

Somit Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, 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.

12
Apna College
123

1234

12345

5. Print Inverted Half Pyramid.

11111

222

33

Apna College

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy