0% found this document useful (0 votes)
16 views15 pages

Patterns Practice Questions

The document contains various programming patterns implemented in Java, including square, right angle, pyramid, inverted pyramid, rhombus, hollow shapes, and more. Each pattern is accompanied by code snippets that demonstrate how to generate the respective shapes using nested loops and conditional statements. The patterns range from simple shapes like squares and triangles to more complex designs like butterflies and diamonds.

Uploaded by

HARISH S ECE
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)
16 views15 pages

Patterns Practice Questions

The document contains various programming patterns implemented in Java, including square, right angle, pyramid, inverted pyramid, rhombus, hollow shapes, and more. Each pattern is accompanied by code snippets that demonstrate how to generate the respective shapes using nested loops and conditional statements. The patterns range from simple shapes like squares and triangles to more complex designs like butterflies and diamonds.

Uploaded by

HARISH S ECE
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
You are on page 1/ 15

Patterns Practice Questions

S.NO

1. Square Pattern:5 import java.util.*;


*****
***** class Main{
***** public static void
***** main(String[] args){
***** Scanner sc = new
Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=1;j<=n;j++){

System.out.print("*");

}
System.out.println();
}
}
}

2. Right Angle star Pattern:5 import java.util.*;


*
** class Main{
*** public static void
**** main(String[] args){
***** Scanner sc = new
Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=1; j<=i;j++){

System.out.print("*");
}
System.out.println();
}
}
}
3. Pyramid Pattern:5 import java.util.*;
* class Main{
*** public static void
***** main(String[] args){
******* Scanner sc = new
********* Scanner(System.in);
int n=sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=i;j<n;j++){
System.out.print("
");
}
for(int
j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
}

4. Inverted Pyramid:5 import java.util.*;


*********
******* class Main{
***** public static void
*** main(String[] args){
* Scanner sc = new
Scanner(System.in);
int n=sc.nextInt();
for(int i=n; i>=1;i--){
for(int j=i; j<n;j++){
System.out.print("
");
}
for(int j=1;
j<=2*i-1;j++){

System.out.print("*");
}
System.out.println();
}
}
}

5. Star Pattern - Rhombus:5 import java.util.*;


*****
***** class Main{
***** public static void
***** main(String[] args){
***** Scanner sc = new
Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=i; j<n;j++){
System.out.print("
");
}
for(int j=1; j<=n;j++){

System.out.print("*");
}
System.out.println();
}
}
}

6. star pattern-single right arrow:11 import java.util.*;

* class Main{
** public static void
*** main(String[] args){
**** Scanner sc = new
***** Scanner(System.in);
****** int n = sc.nextInt();
******* for(int i=1; i<=n;i++){
******** for(int j=1; j<=i;j++){
*********
System.out.print("*");
**********
}
***********
System.out.println();
*********** }
**********
********* for(int i=n; i>=1;i--){
******** for(int j=1; j<=i;j++){
******* System.out.print("*");
****** }
***** System.out.println();
**** }
*** }
** }
*

7. HOLLOW SQUARE5 import java.util.*;


*****
* * class Main{
* * public static void
* * main(String[] args){
***** Scanner sc = new
Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=1; j<=n;j++){
if(i==1 || i==n || j==1
|| j==n){

System.out.print("*");
}
else{
System.out.print("
");
}
}
System.out.println();
}
}
}

8. Cross Pattern5 import java.util.*;


* * class Main{
* * public static void
* * main(String[] args){
** Scanner sc = new
* Scanner(System.in);
** int n = sc.nextInt();
* * n=n*2-1;
* * for(int i=1; i<=n;i++){
* * for(int j=1; j<=n;j++){
if(j==i || j==(n-i+1)){

System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();

}
}
}

9. Hollow square with X pattern:5 import java.util.*;


*********
** ** class Main{
** ** public static void
* * * * main(String[] args){
* * * Scanner sc = new
* * * * Scanner(System.in);
** ** int n=sc.nextInt();
** ** n=2*n-1;
********* for(int i=1; i<=n;i++){
for(int j=1; j<=n;j++) {

if(i==1||i==n||j==1||j==n||j
==i||j==(n-i+1)){

System.out.print("*");
}
else{
System.out.print("
");
}
}
System.out.println();
}
}
}

10. Plus Pattern: import java.util.*;


*
* class Main{
* public static void
******* main(String[] args){
* Scanner sc = new
* Scanner(System.in);
* int n = sc.nextInt();
for(int i=1;i<=n;i++){
for(int j=1; j<=n;j++){
if(i==n/2+1 ||
j==n/2+1){

System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
1. Hill Pattern 🙂
*
***
*****
*******
*********

Import java.util.*;

Class Main{
Public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=i;j<=n;j++){
System.out.print(“ “);
}
for(int j=1; j<i;j++){
System.out.print(“*”);
}
for(int j=1; j<=i;j++){
System.out.print(“*);
}
System.out.println();
}
}
}
2. Reverse Hill Pattern:
*********
*******
*****
***
*

import java.util.*;

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=n; i>=1;i--){
for(int j=i;j<=n;j++){
System.out.print(" ");
}
for(int j=1; j<i;j++){
System.out.print("*");
}
for(int j=1; j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
3. Butterfly Pattern:

* *
** **
*** ***
********
********
*** ***
** **
* *

public class Main {


public static void main(String[] args) {
int n = 4; // Number of rows per wing

// Upper Half
for (int i = 1; i <= n; i++) {
// Left wing stars
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
// Spaces between wings
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Right wing stars
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}

// Lower Half
for (int i = n; i >= 1; i--) {
// Left wing stars
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
// Spaces between wings
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Right wing stars
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

4.
10101
01010
10101
01010
10101

import java.util.*;

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=1; j<=n;j++){
if((i+j )% 2 == 0){
System.out.print("1");
}
else{
System.out.print("0");
}
}
System.out.println();
}
}
}

5.
11111111
10000001
10000001
10000001
10000001
10000001
10000001
11111111

import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n;i++){
for(int j=1; j<=n;j++){
if(i==1 || i==n || j==1 || j==n){
System.out.print("1");
}
else{
System.out.print("0");
}
}
System.out.println();
}
}
}
6.
111101111
111101111
111101111
111101111
000000000
111101111
111101111
111101111
111101111

import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
if(n%2==0)
n--;
for(long i=1; i<=n;i++){
for(long j=1; j<=n;j++){
if(i==n/2+1 || j==n/2+1){
System.out.print("0");
}
else {
System.out.print("1");
}

}
System.out.println();
}
}
}
1.
*****
*****
*****
*****
*****

if(j==1 || j==n)
S.o.p(“*”);
else
S.o.p(“ “);

2.
*
*
*****
*
*
if(i==n/2+1 // j== n/2+1)
S.O.P(“*”);

3.
* *
**
*
**
* *

if(i==j || i+j ==n+1)


S.o.p(“*”);

4. Hollow Square Pattern:


*****
* *
* *
* *
*****
if(i==1 || i==n || j==1 || j== n)
S.o.p(“*”);

5. Hollow Triangle Increasing:


*
**
* *
* *
*******
if(i==n || j==1 || j==i)
S.o.p(“*”);

6. Hollow Triangle Decreasing:


*******
* *
* *
**
*

if(i==1 || j==1 || j==n)


S.o.p(“*”);

7. Hollow Hill Pattern:


*
**
* *
* *
******

if(i==n || j==1)
S.o.p(“*”);
if(i==n || j==i)

8. Diamond pattern
*
**
* *
**
*

if(j==1)

if(j==i)

if(j==i)

if(j==n)

You might also like