Sunteți pe pagina 1din 21

Pattern programs in java : Pattern 1

1
12
123
1234
12345
123456
1234567

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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(j+" ");
}

System.out.println();
}

//Close the resources

sc.close();
}
}

Pattern programs in java : Pattern 2

1
12
123
1234
12345
1234567
123456
12345
1234
123
12
1

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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

int rows = sc.nextInt();

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

//Printing upper half of the pattern

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}

//Printing lower half of the pattern

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 3 

1
22
333
4444
55555
666666
7 7 7 7 7 7 7 

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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(i+" ");
}

System.out.println();
}

//Close the resources

sc.close();
}
}

Pattern programs in java : Pattern 4

7654321
7 6 5 4 3 2 
7 6 5 4 3 
7654
765
76
7

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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 = rows; j >= i; j--)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 5

1234567
123456
12345
1234
123
12
1

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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 = rows; i >= 1; i--)


{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 6

7654321
654321
54321
4321
321
21
1

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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 = rows; i >= 1; i--)


{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 7

7
7 6 
765
7654
76543
7 6 5 4 3 2 
7 6 5 4 3 2 1 

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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 = rows; i >= 1; i--)


{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 8

1
121
12321
1234321
123454321
12345654321
1234567654321

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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++)


{
//Printing first half of the row

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


{
System.out.print(j+" ");
}

//Printing second half of the row


for (int j = i-1; j >= 1; j--)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 9

1234567
123456
12345
1234
123
1 2 

12
1 2 3 
1 2 3 4 
12345
123456
1234567

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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

int rows = sc.nextInt();

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

//Printing upper half of the pattern

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}

//Printing lower half of the pattern


for (int i = 2; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 10

1234567
  234567
    34567
      4567
        567
          67
      7
          67
        567
      4567
    34567
  234567
1234567

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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

int rows = sc.nextInt();

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

//Printing upper half of the pattern

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


{
//Printing i spaces at the beginning of each row

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


{
System.out.print(" ");
}
//Printing i to rows value at the end of each row

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


{
System.out.print(j);
}

System.out.println();
}

//Printing lower half of the pattern

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


{
//Printing i spaces at the beginning of each row

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


{
System.out.print(" ");
}

//Printing i to rows value at the end of each row

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


{
System.out.print(j);
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 11

1
21
321
4321
5 4 3 2 1 
654321
7654321

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user


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 = i; j >= 1; j--)
{
System.out.print(j+" ");
}

System.out.println();
}

//Close the resources

sc.close();
}
}
Pattern programs in java : Pattern 12

1
10
101
1010
10101
101010 
1010101

import java.util.Scanner;

public class MainClass


{
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++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}

System.out.println();
}

sc.close();
}
}

Pattern programs in java : Pattern 13

1  2  3  4  5  6  7 
  2  3  4  5  6  7
    3  4  5  6  7
      4  5  6  7
        5  6  7
          6  7
      7
          6  7
        5  6  7
      4  5  6  7
    3  4  5  6  7
  2  3  4  5  6  7
1  2  3  4  5  6  7

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

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

int rows = sc.nextInt();

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

//Printing upper half of the pattern

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


{
//Printing i spaces at the beginning of each row

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


{
System.out.print(" ");
}

//Printing i to rows value at the end of each row

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


{
System.out.print(j+" ");
}

System.out.println();
}

//Printing lower half of the pattern

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


{
//Printing i spaces at the beginning of each row

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


{
System.out.print(" ");
}

//Printing i to rows value at the end of each row

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


{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}
Pattern programs in java : Pattern 14

1111111
1111122
1111333
1114444
1155555
1666666
7777777 

import java.util.Scanner;

public class MainClass


{
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 <= rows-i; j++)
{
System.out.print(1);
}

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


{
System.out.print(i);
}

System.out.println();
}

sc.close();
}
}
Pattern programs in java : Pattern 15 

1010101
0101010
1010101
0101010
1010101
0101010
1010101

import java.util.Scanner;

public class MainClass


{
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++)


{
int num;

if(i%2 == 0)
{
num = 0;

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


{
System.out.print(num);

num = (num == 0)? 1 : 0;


}
}
else
{
num = 1;

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


{
System.out.print(num);

num = (num == 0)? 1 : 0;


}
}

System.out.println();
}

sc.close();
}
}

Pattern programs in java : Pattern 16

1
26
3 7 10 
4 8 11 13
5 9 12 14 15

import java.util.Scanner;

public class MainClass


{
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++)


{
int num = i;

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


{
System.out.print(num+" ");

num = num+rows-j;
}

System.out.println();
}

sc.close();
}
}
Pattern programs in java : Pattern 17

0000000 
0100000 
0020000
0003000
0000400
0000050
0000006

import java.util.Scanner;

public class MainClass


{
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 = 0; i <= rows; i++)


{
for (int j = 0; j <= rows; j++)
{
if (i == j)
System.out.print(i);
else
System.out.print(0);

System.out.println();
}

sc.close();
}
}

Pattern programs in java : Pattern 18

1
23
456
7 8 9 10
11 12 13 14 15

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
System.out.println("How many rows you want in this pattern?");
Scanner sc = new Scanner(System.in);

int noOfRows = sc.nextInt();

int value = 1;

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

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(value+"\t");

value++;
}

System.out.println();
}
}
}
Pattern programs in java : Pattern 19

1
22
333
4444
5 5 5 5 5 
666666
7 7 7 7 7 7 7 

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
System.out.println("How many rows you want in this pattern");

Scanner sc = new Scanner(System.in);

int noOfRows = sc.nextInt();

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}

System.out.println();
}
}
}
w a
e v
l a
c j
o o
m t
e
m t
o o
c j
l a
e v
w a

class GFG
{
  
// Function to print given 
// string in cross pattern
static void pattern(String str, int len)
{
      
    for (int i = 0; i < len; i++)
    {
        int j = len - 1 - i;
        for (int k = 0; k < len; k++)
        {
            if (k == i || k == j)
                System.out.print(str.charAt(k));
            else
                System.out.print(" ");
        }
        System.out.println("");
    }
}
  
public static void main (String[] args)
{
    String str = "sample";
    int len = str.length();
    pattern(str, len);
  
}
}

import java.io.*;
  
public class One
{
  
    public static void printStars(int n)
    {
        int i, j;
  
        for(i=0; i<n; i++)
        {
  
            
            for(j=0; j<=i; j++)
            {
            
                System.out.print("* ");
            }
  
           
            System.out.println();
        }
   }
  
    
    public static void main(String args[])
    {
        int n = 5;
        printStars(n);
    }
}

Output:
*
* *
* * *
* * * *
* * * * *

import java.io.*;
  
public class Two
{
      public static void printStars(int n)
    {
        int i, j;
  
        for(i=0; i<n; i++)
        {
  

            for(j=2*(n-i); j>=0; j--)


            {
              
                System.out.print(" ");
            }
           

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


            {
                     System.out.print("* ");
            }
              
                 System.out.println();
        }
    }
  
   
    public static void main(String args[])
    {
        int n = 5;
        printStars(n);
    }
}

Output:
*
* *
* * *
* * * *
* * * * *

public class Three


{
    public static void printTriagle(int n)
    {
   
     for (int i=0; i<n; i++)
        {
  

            for (int j=n-i; j>1; j--)


            {
                
                System.out.print(" ");
            }
   

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


            {
 
                System.out.print("* ");
            }
   
  
            System.out.println();
        }
    }
      
    public static void main(String args[])
    {
        int n = 5;
        printTriagle(n);
    }
}
Output:
*
* *
* * *
* * * *
* * * * *

public class Pattern {

public static void main(String[] args) {


int rows = 6, coef = 1;

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


for(int space = 1; space < rows - i; ++space) {
System.out.print(" ");
}

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


if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;

System.out.printf("%4d", coef);
}

System.out.println();
}
}
}

Output

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

S-ar putea să vă placă și