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

Pattern Programming

The document contains Java code examples for various character and number pattern programs. Each section includes multiple classes that demonstrate different patterns using nested loops, with outputs ranging from characters to numbers. The patterns include right-aligned triangles, sequences, and alternating values.

Uploaded by

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

Pattern Programming

The document contains Java code examples for various character and number pattern programs. Each section includes multiple classes that demonstrate different patterns using nested loops, with outputs ranging from characters to numbers. The patterns include right-aligned triangles, sequences, and alternating values.

Uploaded by

R2f Lucifer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Pattern Programming

Character Pattern
1.
class CPat1{
public static void main(String[]args){
int n =5;
// row
for(int i =0;i<n;i++){
//col
for(int j =0;j<n;j++){
if(i>=j){
System.out.print((char)('a'+j)+" ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
//a
//a b
//a b c
//a b c d
//a b c d e
2.

class CPat2{

public static void main(String[]args){

int n = 5;

// row

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

// col

for(int j =0;j<n;j++){

if(i>=j){

System.out.print((char)('a'+i)+" ");

}else {

System.out.print(" ");

System.out.println();

//a

//b b

//c c c

//d d d d

//e e e e e
3.

class CPat3{

public static void main(String[]args){

int n = 5;

char ch = 'a';

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

for(int j=0;j<n;j++){

if(i>=j){

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

}else{

System.out.print(" ");

System.out.println();

//a

//b c

//d e f

//g h i j

//k l m n o
4.

class CPat4{

public static void main(String []args){

int n = 5;

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

for(int j=0;j<n;j++){

if(i>=j){

System.out.print((char)('a'+i+j)+" ");

}else{

System.out.print(" ");

System.out.println();

//a

//b c

//c d e

//d e f g

//e f g h i
Number Pattern
1.

class NPat1{

public static void main(String[]agrs){

int n = 5;

int k =0;

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

for(int j =0;j<n;j++){

if(i>=j){

System.out.print(k++%10 +" ");

}else{

System.out.print(" ");

} System.out.println();

//0

//1 2

//3 4 5

//6 7 8 9

//0 1 2 3 4
2.

class NPat2{

public static void main(String[]args){

int n = 5;

int k=9;

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

for(int j=0;j<n;j++){

if (i>=j){

System.out.print((k++%9)+1 +" ");

}else{

System.out.print(" ");

System.out.println();

//1

//2 3

//4 5 6

//7 8 9 1

//2 3 4 5 6
3.

class NPat3{

public static void main(String[]args){

int n = 5;

int k = 10;

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

for(int j=0;j<n;j++){

if(i>=j){

System.out.print((k++%5)+1+" ");

}else{

System.out.print(" ");

System.out.println();

//1

//2 3

//4 5 1

//2 3 4 5

//1 2 3 4 5
4.

class NPat4{

public static void main(String[]args){

int n = 5;

int k =0;

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

for(int j=0;j<n;j++){

if(i>=j){

System.out.print(k++%6+" ");

}else{

System.out.print(" ");

System.out.println();

//0

//1 2

//3 4 5

//0 1 2 3

//4 5 0 1 2
5.

class Pat1{

public static void main(String[]args){

int n =5;

// row

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

// col

for(int j =0;j<n;j++){

if(i>=j){

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

}else{

System.out.print(" ");

System.out.println();

11

222

3333

44444
6.

class Pat2{

public static void main(String[]args){

int n =5;

// row

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

// col

for(int j =0;j<n;j++){

if(i>=j){

System.out.print(j%2+" ");

}else{

System.out.print(" ");

System.out.println();

01

010

0101

01010
7.

class Pat3{

public static void main(String[]args){

int n =5;

// row

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

// col

for(int j =0;j<n;j++){

if(i>=j){

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

}else{

System.out.print(" ");

System.out.println();

11

000

1111

00000
8.

class Pat4{

public static void main(String[]args){

int n =5;

// row

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

//col

for(int j =0;j<n;j++){

if(i>=j){

System.out.print(j+1+" ");

}else{

System.out.print(" ");

System.out.println();

12

123

1234

12345
9.

class Pat5{

public static void main(String[]args){

int n =5;

// row

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

//col

for(int j =0;j<n;j++){

if(i>=j){

System.out.print(j%2+" ");

}else{

System.out.print(" ");

System.out.println();

01

010

0101

01010
10.

class Pat6{

public static void main(String[]args){

int n =5;

// row

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

//col

for(int j =0;j<n;j++){

if(i>=j){

System.out.print((j+1)%2+" ");

}else{

System.out.print(" ");

System.out.println();

10

101

1010

10101
11.

class Pat7{

public static void main(String[]args){

int n =5;

// row

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

//col

for(int j =0;j<n;j++){

if(i>=j){

System.out.print(n-j+" ");

}else{

System.out.print(" ");

System.out.println();

54

543

5432

54321
12.

class Pat8{

public static void main(String[]args){

int n =5;

// row

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

//col

for(int j =0;j<n;j++){

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

System.out.print(j+1+" ");

}else{

System.out.print(" ");

System.out.println();

12345

1234

123

12

1
13.

class Pat9{

public static void main(String[]args){

int n =5;

// row

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

//col

for(int j =0;j<n;j++){

if(i>=j){

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

}else{

System.out.print(" ");

System.out.println();

10

210

3210

43210

You might also like