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

Coding Assignment 18-WPS Office

The document contains 7 code snippets that print different patterns using loops and conditionals in Java. The patterns include triangles, diamonds, numbers, and letters. It also includes code to print an empty box pattern a specified number of times based on user input.

Uploaded by

tamj tamj
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Coding Assignment 18-WPS Office

The document contains 7 code snippets that print different patterns using loops and conditionals in Java. The patterns include triangles, diamonds, numbers, and letters. It also includes code to print an empty box pattern a specified number of times based on user input.

Uploaded by

tamj tamj
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment 18

No. 2

import java.util.Scanner;

public class pattern2

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");

for (int i = 1; i <= rows; i++)

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

System.out.print(" *");

System.out.println();
}

sc.close();

Number 4

public class Pattern4 {

public static void main(String[] args) {

int rows = 5, k = 0;

for(int i = 1; i <= rows; ++i, k = 0) {

for(int space = 1; space <= rows - i; ++space) {

System.out.print(" ");

while(k != 2 * i - 1) {

System.out.print("* ");

++k;

System.out.println();

}
Number 3.

public class Pattern3 {

public static void main(String[] args) {

int val = 5;

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

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

System.out.print(" ");

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

System.out.print("*");

System.out.println("");

Number 6.

import java.util.*;

class pattern6

{
public static void main(String args[])

int i,j,n;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the no of lines");

n=sc.nextInt();

for(i=1;i<=n;i++)

for(j=1;j<=n-i;j++)

System.out.printf(" ");

for(j=i;j>0;j--)

System.out.printf(" %d",j);

for(j=2;j<=i;j++)

System.out.printf(" %d",j);

System.out.println();

}
}

Number5.

import java.util.Scanner;

public class Pattern5

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows to print the pattern ");

int rows = scanner.nextInt();

System.out.println("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)

for (int j = rows; j > i; j--)

System.out.print(" ");

for (int k = 1; k <= i; k++)


{

System.out.print(i + " ");

System.out.println();

Number1.

import java.util.Scanner;

public class Pattern1

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows to print the pattern ");

int rows = scanner.nextInt();

int temp = 0;

System.out.println("** Printing the pattern... **");

for (int i = rows; i >= 1; i--)

{
for (int j = rows ; j >= i; j--)

System.out.print( " *");

temp =j;

for (int k = rows - i+1; k < rows; k++)

System.out.print( " *");

System.out.println();

Part 2

Letter Z :

public class Star {


public static void main(String[] args) {

for (int i = 0; i <=7; i++) {


for (int j = 0; j <=7; j++)
{
if (i==7 || (i+j)==7 || i==0)
{
System.out.print(" # ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}

Empty box:

import java.util.Scanner;

public class SquarePattern {

public static void main(String[] args) {

String pattern;
int noOfTimes;

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of times it should get printed : ");


noOfTimes = scanner.nextInt();

for(int i=2; i<=noOfTimes; i++) {

System.out.println();

if(i==2 || i==noOfTimes) {

for(int j=2; j<=noOfTimes; j++){

System.out.print(" #");

}
}
else {

for(int k=2; k<=noOfTimes;k++) {

if(k==2 || k == noOfTimes) {

System.out.print( " #");

}
else {

System.out.print(" ");

}
}
}
}
}
}

You might also like