GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — Java Program to Print Pattern of Numbers

  • Java Basic Programs
  • Java Programming Examples
  • Java Print Hello World
  • Java Get Input from User
  • Java Print Integer
  • Java Add two Numbers
  • Java Check Even or Odd
  • Java Check Prime or Not
  • Java Check Alphabet or Not
  • Java Check Vowel or Not
  • Check Reverse equal Original
  • Java Fahrenheit to Celsius
  • Java Celsius to Fahrenheit
  • Java Perfect Number Program
  • Java Find Quotient Remainder
  • Java Days to Seconds
  • Java Count Digits in Number
  • Java Binary Number Addition
  • Java Discount Program
  • Java Compute Courier Charge
  • Java Find Telephone Bill
  • Java Print ASCII Values
  • Java Check Palindrome or Not
  • Java Check Armstrong or Not
  • Generate Armstrong Numbers
  • Add two Numbers using Pointers
  • Java Mathematical Programs
  • Add Subtract Multiply & Divide
  • Java Make Calculator
  • Java Add Digits of Number
  • Java Check Leap Year or Not
  • Java Check Divisibility
  • Java Find Simple Interest
  • Java Find Compound Interest
  • Java Print Fibonacci Series
  • Java Find nCr nPr
  • Calculate Average & Percentage
  • Java Calculate Arithmetic Mean
  • Java Calculate Student Grade
  • Java Print Table of Number
  • Java Print Prime Numbers
  • Java Add n Numbers
  • Java Interchange two Numbers
  • Java Reverse Numbers
  • Java Swap two Numbers
  • Count Positive Negative & Zero
  • Find Largest of two Numbers
  • Find Largest of three Numbers
  • Java Find Factorial of Number
  • Java Find HCF & LCM
  • Area & Perimeter of Square
  • Area & Perimeter of Rectangle
  • Area & Circumference of Circle
  • Java Conversion Programs
  • Java Decimal to Binary
  • Java Decimal to Octal
  • Java Decimal to Hexadecimal
  • Java Binary to Decimal
  • Java Binary to Octal
  • Java Binary to Hexadecimal
  • Java Octal to Decimal
  • Java Octal to Binary
  • Java Octal to Hexadecimal
  • Java Hexadecimal to Decimal
  • Java Hexadecimal to Binary
  • Java Hexadecimal to Octal
  • Java Pattern Programs
  • Java Pattern of Stars
  • Java Pattern of Alphabets
  • Java Pattern of Numbers
  • Java Pyramid of Stars
  • Java Pyramid of Alphabets
  • Java Pyramid of Numbers
  • Java Print Diamond Pattern
  • Java Print Floyd Triangle
  • Java Print Pascal Triangle
  • Java Array Programs
  • One Dimensional Array Program
  • Java Linear Search
  • Java Binary Search
  • Find Largest Element in Array
  • Find Smallest Element in Array
  • Java Reverse Array
  • Insert Element in Array
  • Delete Element from Array
  • Java Merge two Array
  • Java Bubble Sort
  • Java Selection Sort
  • Java Insertion Sort
  • Java Find Common Elements
  • Java Count Even/Odd Number
  • Two Dimensional Array Program
  • Java Add two Matrices
  • Java Subtract two Matrices
  • Java Transpose Matrix
  • Multiply two Matrices
  • Three Dimension Array Program
  • Java String Programs
  • Java Print String
  • Find Length of String
  • Java Compare two String
  • Java Copy String
  • Java Concatenate String
  • Java Reverse String
  • Delete Vowels from String
  • Delete Words from Sentence
  • Find Occurrence of a Character
  • Java Find Occurrence of a Word
  • Occurrence of Each Character
  • Java Occurrence of Each Word
  • Java Count Repeated Characters
  • Java Count Repeated Words
  • Java Capitalize Each Word
  • Java Count Vowels/Consonants
  • Java Extract Numbers
  • Java Count Word in String
  • Remove Spaces from String
  • Java Sort a String
  • Java Uppercase to Lowercase
  • Java Lowercase to Uppercase
  • Java Swap two Strings
  • Java Check Anagram or Not
  • Java Check Balance Parentheses
  • Java Check Password Strength
  • Java File Programs
  • Java Read File
  • Java Write to File
  • Read & Display File Content
  • Java Copy File
  • Java Append Text to File
  • Java Merge two File
  • List files in Directory
  • Java Delete File
  • Java Miscellaneous Programs
  • Generate Random Numbers
  • Java Print Time & Date
  • Java Get IP Address
  • Java Shutdown Computer
  • Java Programming Tutorial
  • Java Tutorial

Java Program to Print Pattern of Numbers

This post covers all famous programs in Java that are used to print pattern of numbers. There are almost more than 7 pattern programs using numbers are created here.

Number Pattern Program in Java - Pattern No.1

The question is, write a Java program to print pattern of number. Following program is its answer:

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row=10, num=1;
      
      for(int i=0; i<row; i++)
      {
         for(int j=0; j<=i; j++)
            System.out.print(num+ " ");
         System.out.print("\n");
      }
   }
}

The snapshot given below shows the sample output of above Java program on printing of number pattern:

java print number pattern

Print Number Pattern based on User Input

The previous program can also be created in a way to allow user to define the size of pattern along with the number to use, while forming the pattern:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row, num;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the Row Size of Pattern: ");
      row = s.nextInt();
      System.out.print("Enter the Number to Form Pattern: ");
      num = s.nextInt();
      
      for(int i=0; i<row; i++)
      {
         for(int j=0; j<=i; j++)
            System.out.print(num+ " ");
         System.out.print("\n");
      }
   }
}

The sample run of above program with user input 5 as row size and 4 as number, is shown in the snapshot given below:

java print pattern of numbers

Note - You can use the same process to print the pattern of numbers based on user-input, in other programs given below.

Number Pattern Program in Java - Pattern No.2

The program given below prints right-angled triangle of natural numbers. That is, 1 at first row, 2 3 at second row, 4 5 6 at third row, and so on:

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10, num=1;
      
      for(int i=0; i<r; i++)
      {
         for(int j=0; j<=i; j++)
         {
            System.out.print(num+ " ");
            num++;
         }
         System.out.print("\n");
      }
   }
}

Output of Previous Number Pattern Program

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 

Number Pattern Program in Java - Pattern No.3

Now this program prints pattern of numbers, where each row contains natural numbers. That is, 1 at first row, 1 2 at second row, 1 2 3 at third row, and so on.

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10;
      
      for(int i=0; i<r; i++)
      {
         int num=1;
         for(int j=0; j<=i; j++)
         {
            System.out.print(num+ " ");
            num++;
         }
         System.out.print("\n");
      }
   }
}

Output of Previous Number Pattern Program

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 

Number Pattern Program in Java - Pattern No.4

This program also prints the same pattern as of previous, but this time, 1 at first row, 2 2 at second row, 3 3 3 at third row, and so on.

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=9, num=1;
      
      for(int i=0; i<r; i++)
      {
         for(int j=0; j<=i; j++)
            System.out.print(num+ " ");
         
         System.out.print("\n");
         num++;
      }
   }
}

Output of Previous Number Pattern Program

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 

Number Pattern Program in Java - Pattern No.5

Now let me create some other pattern program of numbers in Java.

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10, spaceLimit, num=1;
      spaceLimit = (r*2) - 2;
      
      for(int i=0; i<r; i++)
      {
         for(int space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(int j=0; j<=i; j++)
            System.out.print(num+ " ");
         System.out.print("\n");
         spaceLimit = spaceLimit-2;
      }
   }
}

Output of Previous Number Pattern Program

                  1 
                1 1 
              1 1 1 
            1 1 1 1 
          1 1 1 1 1 
        1 1 1 1 1 1 
      1 1 1 1 1 1 1 
    1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 

Number Pattern Program in Java - Pattern No.6

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10, num=1;
      
      for(int i=0; i<r; i++)
      {
         for(int j=i; j<r; j++)
            System.out.print(num+ " ");
         
         System.out.print("\n");
      }
   }
}

Output of Previous Number Pattern Program

1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 
1 1 1 
1 1 
1 

Number Pattern Program in Java - Pattern No.7

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10, limit=0, num=1;
      
      for(int i=0; i<r; i++)
      {
         for(int s=0; s<limit; s++)
            System.out.print(" ");
         
         for(int j=i; j<r; j++)
            System.out.print(num+ " ");
         
         System.out.print("\n");
         limit += 2;
      }
   }
}

Output of Previous Number Pattern Program

1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 
    1 1 1 1 1 1 1 1 
      1 1 1 1 1 1 1 
        1 1 1 1 1 1 
          1 1 1 1 1 
            1 1 1 1 
              1 1 1 
                1 1 
                  1 

Number Pattern Program in Java - Pattern No.8

This program is used to print equilateral triangle using a 1, a number.

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10, num=1;
      
      for(int i=0; i<r; i++)
      {
         for(int s=i; s<r; s++)
            System.out.print(" ");
         
         for(int j=0; j<=i; j++)
            System.out.print(num+ " ");
         
         System.out.print("\n");
      }
   }
}

Output of Previous Number Pattern Program

          1 
         1 1 
        1 1 1 
       1 1 1 1 
      1 1 1 1 1 
     1 1 1 1 1 1 
    1 1 1 1 1 1 1 
   1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 
 1 1 1 1 1 1 1 1 1 1 

Number Pattern Program in Java - Pattern No.9

public class CodesCracker
{
   public static void main(String[] args)
   {
      int r=10, num=1;
      
      for(int i=0; i<r; i++)
      {
         for(int s=0; s<i; s++)
            System.out.print(" ");
         
         for(int j=i; j<r; j++)
            System.out.print(num+ " ");
         
         System.out.print("\n");
      }
   }
}

Output of Previous Number Pattern Program

1 1 1 1 1 1 1 1 1 1 
 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 
   1 1 1 1 1 1 1 
    1 1 1 1 1 1 
     1 1 1 1 1 
      1 1 1 1 
       1 1 1 
        1 1 
         1 

Note - For more pattern, refer to Star Pattern Program in Java. The only change, you need to do, is to change the star with number.

Java Online Test


« Previous Program Next Program »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.