Q:

Java program to print patterns (2 Examples based on numbers pattern)

belongs to collection: Java Basic Programs

0

Pattern 1

    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 

Pattern 2

    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 

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to print pattern 1 in java

import java.util.Scanner;

public class Pattern27 
{
	public static void main(String[] args) 
    {
		// create scanner class object.
        Scanner sc = new Scanner(System.in);
         
        // input row for printing pattern.
        System.out.print("Enter row for pattern : ");
         
        int rows = sc.nextInt();
        
        System.out.println("Here is your pattern....!!!");
         
        // loop for printing pattern.
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }            
            System.out.println();
        }
        sc.close();
    }
}

Output

Enter row for pattern : 10
Here is your pattern....!!!
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 

Program to print pattern 2 in java

import java.util.Scanner;

public class Pattern28 
{
	public static void main(String[] args) 
    {
		// create scanner class object.
        Scanner sc = new Scanner(System.in);
         
        // input row for printing pattern.
        System.out.print("Enter row for pattern : ");
         
        int rows = sc.nextInt();
         
        System.out.println("Here is your pattern....!!!");
        
        // this loop will print the pattern in two parts first half and second half.
        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();
        }
        sc.close();
    }
}

Output

Enter row for pattern : 5
Here is your pattern....!!!
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 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find perimeter of a rectangle... >>
<< Java program to print prime numbers between given ...