Print Number Star Square Pattern Type 3

PRINT PATTERN  1*2*3*4  9*10*11*12

For any input number N Print the following code – For below code N=4

1*2*3*4
9*10*11*12
5*6*7*8
13*14*15*16

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

  1. Take input from user i.e number of lines required (N value).
  2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’). i starts from 1 and j starts from 1.
  3. Take a result variable (say ‘a’) and initialize it with 1.
  4. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values in each line. Entire pattern is divided in to even and odd lines. For odd lines it is printed sequentially and for even lines ‘a’ value is incremented by n.
  5. Print ‘a’ value along with * and post increment  until the j loop reaches a value less than n.
  6. Print the final value ‘a’ of each line.
  7. Repeat the ‘i’ loop until it reaches n.

CODE IN JAVA:

[code language=”java”]
import java.lang.*;
import java.io.*;
class Pattern
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a=1,t;
System.out.print("Enter N value:");
n=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{
if(i%2==0)
{
t=a+n;
for(j=1;j<n;j++)
{
System.out.print((t++)+"*");
}
System.out.println(t++);
}
else
{
for(j=1;j<n;j++)
{
System.out.print((a++)+"*");
}
System.out.println(a++);
}
}
}
}
[/code]

 

TAKING INPUT:

DISPLAYING OUTPUT:

 

 

4 comments on “Print Number Star Square Pattern Type 3”


  • npkrish1122

    import java.util.*;
    class Main {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int num=1;
    int n1=(n*n)/2+1;
    for(int i=1;i<=n;i++){
    String s="";
    if(i%2==1){
    for(int k=1;k<n;k++){
    s+=num+"*";
    num++;
    }
    System.out.println(s+num);
    num++;
    }else{
    for(int j=1;j<n;j++){
    s+=n1+"*";
    n1++;
    }
    System.out.println(s+n1);
    n1++;
    }}}}
    //output:
    4
    1*2*3*4
    9*10*11*12
    5*6*7*8
    13*14*15*16


  • Ajitha

    Python
    n=4
    start1=1
    for i in range(1,n+1):
    num1=start1
    for j in range(1,n+1):
    print(num1,end=””)
    if j<n:
    print("*",end="")
    num1+=1
    print()
    if i%2!=0:
    start1=start1+2*n
    else:
    start1=start1-n


  • 2100049006ece

    import java.util.*;
    public class pattern{
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    System.out.print(“enter number of rows=”);
    int n=sc.nextInt();
    int k=1;
    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    if(j!=n-1)
    System.out.print((k++)+"*");
    else
    System.out.print(k++);
    }
    System.out.println();
    if(i%2==0)
    k=k+4;
    else
    k=k-8;
    }
    }
    }


  • Sudip

    import java.io.*;

    class Main {
    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n, m, i, j, a, p = 1, q = 9, v, c;

    System.out.print(“Enter N value: “);
    n = Integer.parseInt(br.readLine());

    System.out.print(“Enter M value: “);
    m = Integer.parseInt(br.readLine());

    for (i = 1; i <= n; i++) {
    c = 1;
    v = 1;
    for (j = 1; j <= m; j++) {
    if (i % 2 != 0) {
    System.out.print(p);
    c++;
    p++;
    if (c <= m) {
    System.out.print("*");
    }
    } else {
    System.out.print(q);
    q++;
    v++;
    if (v <= m) {
    System.out.print("*");
    }
    }
    }
    System.out.println();
    }
    }
    }