Java program to print number star pattern type 1

Print number star pattern type 1

Take input from user i.e number of lines required (N value). Take a result variable (say ‘a’) and initialize it with 1.Take two loops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’).Here ‘i’ loop is used to access each line from 1 to N and ‘j’ loop is used to print values in each line. For example in line 2 the value of i=2 and for contents in second line (i.e 5*6*7*8) the value for j for digit 5 is j=1 and for digit 6 is j=2.Print ‘a’ value along with * and post increment it until the j loop reaches a value less than n After the j loop is finished, print the ‘a’ value without * and post increment it and go to next line.Repeat the ‘i’ loop until it reaches ‘N’ lines.

Java program to print Number Star Pattern Type 1

Algorithm

  • Take input from user i.e number of lines required (N value).
  • Take a result variable (say ‘a’) and initialize it with 1.
  • Take two loops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’).
  • Here ‘i’ loop is used to access each line from 1 to N and ‘j’ loop is used to print values in each line. For example in line 2 the value of i=2 and for contents in second line (i.e 5*6*7*8) the value for j for digit 5 is j=1 and for digit 6 is j=2.
  • Print ‘a’ value along with * and post increment it until the j loop reaches a value less than n.
  • After the j loop is finished, print the ‘a’ value without * and post increment it and go to next line.
  • Repeat the ‘i’ loop until it reaches ‘N’ lines.

Code in Java

import java.io.*;
classPrepInsta{
   public static void main(String[] args) throws IOException {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   int n, i, j, a = 1;
   System.out.print("Enter the number of Lines:");
   n = Integer.parseInt(br.readLine()); 
   for (i = 1; i <= n; i++){
     for (j = 1; j < n; j++) {
       System.out.print((a++) + "*");
     }
     System.out.println(a++);
   }
 }
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)