CodesJava

Easy learning with example program codes

java number pattern programs


Pattern Example-1

Pattern

----Pattern is----
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);      
        System.out.println("Number of rows you want in this pattern?");         
        int rows = scanner.nextInt();         
        System.out.println("----Pattern is----");         
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Pattern Example-2

Pattern

----Pattern is----
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test
{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);         
        System.out.println("Number of rows you want in this pattern?");         
        int rows = scanner.nextInt();         
        System.out.println("----Pattern is----");         
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(i+" ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5

Pattern Example-3

Pattern

----Pattern is----
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Number of rows you want in this pattern?");         
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++) 
            { 
                System.out.print(j+" "); 
            } 
 
            System.out.println(); 
        } 
 
        // 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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

Pattern Example-4

Pattern

----Pattern is----
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);        
        System.out.println("Number of rows you want in this pattern?");         
        int rows = scanner.nextInt();         
        System.out.println("----Pattern is----");         
        for (int i = rows; i >= 1; i--) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

Pattern Example-5

Pattern

----Pattern is----
5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
        for (int i = 1; i <= rows; i++) 
        { 
            for (int j = rows; j >= i; j--)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5

Pattern Example-6

Pattern

----Pattern is----
5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1 

package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
        for (int i = rows; i >= 1; i--) 
        {
            for (int j = rows; j >= i; j--)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1

Pattern Example-7

Pattern

----Pattern is----
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 

package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
        for (int i = rows; i >= 1; i--) 
        {
            for (int j = i; j >= 1; j--)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

Pattern Example-8

Pattern

----Pattern is----
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Number of rows you want in this pattern?");
        int rows = scanner.nextInt();         
        System.out.println("----Pattern is----");
 
        for (int i = rows; i >= 1; i--) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
        for (int i = 2; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Pattern Example-9

Pattern

----Pattern is----
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 

package com.codesjava;
 
import java.util.Scanner;
 
public class Test
{
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++) 
            { 
                System.out.print(j+" "); 
            }
 
            for (int j = i-1; j >= 1; j--)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1

Pattern Example-10

Pattern

----Pattern is----
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = i; j >= 1; j--)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

Pattern Example-11

Pattern

----Pattern is----
12345
 2345
  345
   45
    5
   45
  345
 2345
12345
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
        for (int i = 1; i <= rows; i++) 
        {
 
 
            for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }
 
 
 
            for (int j = i; j <= rows; j++) 
            { 
                System.out.print(j); 
            } 
 
            System.out.println(); 
        } 
 
 
 
        for (int i = rows-1; i >= 1; i--) 
        {
 
 
            for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }
 
 
 
            for (int j = i; j <= rows; j++)
            {
                System.out.print(j);
            }
 
            System.out.println();
        }
 
 
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
12345
 2345
  345
   45
    5
   45
  345
 2345
12345

Pattern Example-12

Pattern

----Pattern is----
1 2 3 4 5 
 2 3 4 5 
  3 4 5 
   4 5 
    5 
   4 5 
  3 4 5 
 2 3 4 5 
1 2 3 4 5 
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
 
        System.out.println("----Pattern is----");
 
 
 
        for (int i = 1; i <= rows; i++) 
        {
 
 
            for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }
 
 
 
            for (int j = i; j <= rows; j++) 
            { 
                System.out.print(j+" "); 
            } 
 
            System.out.println(); 
        } 
 
 
 
        for (int i = rows-1; i >= 1; i--) 
        {
 
            for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }
 
 
            for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }
 
            System.out.println();
        }
 
 
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 2 3 4 5 
 2 3 4 5 
  3 4 5 
   4 5 
    5 
   4 5 
  3 4 5 
 2 3 4 5 
1 2 3 4 5

Pattern Example-13

Pattern

----Pattern is----
1
10
101
1010
10101
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
        System.out.println("----Pattern is----");
 
        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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1
10
101
1010
10101

Pattern Example-14

Pattern

----Pattern is----
10101
01010
10101
01010
10101
package com.codesjava;
 
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
        System.out.println("----Pattern is----");
        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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
10101
01010
10101
01010
10101

Pattern Example-15

Pattern

----Pattern is----
11111
11122
11333
14444
55555

package com.codesjava;
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
        System.out.println("----Pattern is----");
        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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
11111
11122
11333
14444
55555

Pattern Example-16

Pattern

----Pattern is----
00000
01000
00200
00030
00004

package com.codesjava;
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
        System.out.println("----Pattern is----");
 
        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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
00000
01000
00200
00030
00004

Pattern Example-17

Pattern

----Pattern is----
1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15 
package com.codesjava;
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
        System.out.println("----Pattern is----"); 
        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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15

Pattern Example-18

Pattern

----Pattern is----
1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15 
package com.codesjava;
import java.util.Scanner;
 
public class Test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Number of rows you want in this pattern?");
 
        int rows = scanner.nextInt();
        System.out.println("----Pattern is----"); 
        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();
        }
 
        scanner.close();
    }
}

Output

Number of rows you want in this pattern?
5
----Pattern is----
1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15
Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki