How to construct a number pattern program using for loop in java?

Introduction:

Hii everyone, here we are going to make a program on number pattern program.As we can see the pattern program are not much difficult to construct it just needs your attention and analysis skills.For better understanding use a pen and a paper and plot the required figure to analyse it correctly.

Description Of Program:

First of all, here we are making our program user flexible so we will input scanner first and then define its data type being used as we are taking the integer as a data type.Moving forward we will take four loops here,”i” loop we will run for row and it will run up to “n” moving inside the bracket “j” loop will be running for spaces being used in the program,”k1″ loop will be running up to “i” and then “k2” loop will be run in the opposite direction as the pattern being used demands the loop should be run in reverse direction.Hope you find it helpful.                                                                                          Thank You!

CODE :

package patternspackage;

import java.util.Scanner;
public class NumberPattern {
public static void main(String[] args) {
System.out.println(“Enter the value of N”);
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
System.out.print(” “);
for(int k1=1;k1<=i;k1++)
System.out.print(k1);
for(int k2=i-1;k2>=1;k2–)
System.out.print(k2);
System.out.println();
}

}
}

OUTPUT :

Leave a Reply