Creating Number Patterns In Java

Creating Number Patterns In Java

Number Pattern in Java
Creating number or symbol patterns is used to check command of a programmer or a student on for loop. In short its done just for practicing for loop.


Here are some common number patterns :

Pattern 1. 
Pattern 1
To Print this pattern use the following code:

public class numPtr {
    public static void main( String arg[]){
        for(int i=1;i<=5;i++){

            for(int j=1;j<=i;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Pattern 2. 
Pattern 2
To Print this pattern use the following code:

public class numPtr {
    public static void main( String arg[]){
            for(int i=1;i<=5;i++){
            for(int j=5;j>=i;j--){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Pattern 3. 
Pattern 3
To Print this pattern use the following code:
public class numPtr {
    public static void main( String arg[]){
           for(int i=1,r=5;i<=5;i++,r--){
            for(int j=1;j<=r;j++){
                System.out.print(j);
            }
            System.out.println();        }
    }
}

Pattern 4. 
Pattern 4
To Print this pattern use the following code:
public class numPtr {
    public static void main( String arg[]){
int ck=0,c=2;
        while(c>0){
        if(ck==0){
            for(int i=1;i<=5;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j);
            }
            System.out.println();         }
            ck++;
       } else{
            for(int i=1,r=5-1;i<=5-1;i++,r--){
            for(int j=1;j<=r;j++){
                System.out.print(j);            }
            System.out.println();
        }
        }
        c--;
      }

    }
}

Pattern 5. 

Pattern 5
To Print this pattern use the following code:
public class numPtr {
    public static void main( String arg[]){
int ck=0,c=2;
        while(c>0){
        if(ck==0){
            for(int i=1,r=5;i<=5;i++,r--){
            for(int j=1;j<=r;j++){
                System.out.print(j);
            }
            System.out.println();        }
            ck++;
       } else{
            for(int i=2;i<=5;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j);            }
            System.out.println();
        }
        }
        c--;
      }

    }
}
Author Note: All the above examples gives output till 5, you can change it accordingly by changing the conditions. 

1230 Comments Leave new

«Oldest   ‹Older   1201 – 1230 of 1230   Newer›   Newest»

class Sample
{
public static void main ( String arg[] )
{
int k=0;
int l=k;
int n=k;
for(int i =1;i<=4;i++)
{

for(int j=1;j<=4;j++)
{
if (i==1 )
{
k=k+1;
if(k==4)
{
k=2;

}
System.out.print(k+" ");

}
else if(i ==3)
{
l=l+1;
if(l==4)
{
l=2;

}
System.out.print(l+" ");
}
else if((i==2 || i ==4) && (j==1))
{
System.out.print(2+" ");
}
else if(i==2)
{
n = n+1;
System.out.print(n+" ");
}
else
{
n = n+1;
System.out.print(n+" ");
}

}
n=0;
System.out.println();
}
}
}

Reply

Public class Piyush{
public static void main(String args[]){
For(int I=1;I<=5;I++){
System.out.print(I);
For(int j=I;j>=1;j---){
System.out.print(j);
}
System.out.println();
}
}
}

Reply

public static void main(String args[])
{
int l = 1;
for(int i=1;i<=5;i++)
{
for(int z=1;z<=i;z++)
{
System.out.print(z);
}
if(i<5)
{
System.out.print(" ");
}
for(int x =l;x>=1;x--)
{
System.out.print(x);
}
l++;
System.out.println();
}
}
}

Reply

public static void main(String args[])
{
int l = 1;
for(int i=1;i<=5;i++)
{
for(int z=1;z<=i;z++)
{
System.out.print(z);
}
if(i<5)
{
System.out.print(" ");
}
for(int x =l;x>=1;x--)
{
System.out.print(x);
}
l++;
System.out.println();
}
}
}

Reply

public static void main(String args[])
{
int l = 1;
for(int i=1;i<=5;i++)
{
for(int z=1;z<=i;z++)
{
System.out.print(z);
}
if(i<5)
{
System.out.print(" ");
}
for(int x =l;x>=1;x--)
{
System.out.print(x);
}
l++;
System.out.println();
}
}
}

Reply

can you help me for this pattern

1
121
12321
1234321
123454321

Reply
This comment has been removed by the author.

can you help me out to get the following logics?
1
ABC
45678
DEFGHIJ

Reply
This comment has been removed by the author.

int n,i,j;
printf("enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==0)
{
if(j%2!=0)
{
printf("0");
}
else
printf("1");
}
if(i%2!=0)
{
if(j%2==0)
printf("0");
else
printf("1");
}
}
printf("\n");
}

Reply

please help me to code this:
1
32
654
10987

Reply
This comment has been removed by the author.

how to make the following patterns :-

a) 1 2 3 4 5
4 5 6
7 8
15





b) 2
3 4
5 6 7
7 8 9 10
11 12 13 14 15
13 14 15 16 17 18

Reply

hi im kartik,pls tell this

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

Reply

What will be logic for :
0
1 2
3 4 5
6 7 8 9

Reply

please code for this

123
45678
99876
54321
123

Reply

Write a Java Program to that prints the following output using single for loop.

20 50
15 55
10 65
5 70
0 75

Pls help me in this one

Reply

can you help to get this output
input:7
output:
1010101
0101010
1010101
0101010
1010101
0101010
1010101

Reply

5
5 10
5 10 15
Please give me answer

Reply

import java.util.*;
public class five {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(5*j+" ");
}
System.out.println();
}
}

}


Put n as number of lines. In your case, n=3.

Reply
This comment has been removed by the author.

import java.util.*;
public class five {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
for(int i=1;i<=n;i++)
{
if(i%2==0)
{
for(int j=1;j<=n;j++)
{
if(j%2==0)
System.out.print("1");
else
System.out.print("0");
}
}
else
{
for(int j=1;j<=n;j++)
{
if(j%2==0)
System.out.print("0");
else
System.out.print("1");
}
}
System.out.println();
}
}

}

Reply

Hii Sir,
Below program is giving me output like this:
5
53
531
but, I want output like this:
5
53
531

Syntax:

package Patterns;

public class Pttn5
{
public static void main(String[] args)
{
for (int i=1; i<=1; i++)
{
int m=3;
while(m>1)
{
int j=m-1;
{
System.out.print(" ");
}
m=m-1;
}

int a=4;
while(a>=0)
{
for (int k=5; k>=a; k--)
{
if (k%2==0)
{
System.out.print("");
}
else
{
System.out.print(k+"");
}
}
a=a-2;
System.out.println("");
}
}
}
}

So can you, pls help me to correct the error

Reply

I do not understand what you want and what are you looking for.

Also, your code looks quite complex. I think it is unnecessarily complex for a simple program, using many loops.

Assuming I understood your program correctly, here is a much simpler version.

https://onlinegdb.com/rkBCT_VI7

public class Main
{
public static void main(String[] args) {
int m = 5;
for (int i=m; i>=1; i--)
{
for (int j=0; j<=m-i; j++)
System.out.print(2*(m-j)-1);
System.out.println();
}
}
}

Reply

Hi please solve this pattern
1
2 9
3 10 16
4 11 17 22
5 12 18 23 27

Thankyou...

Reply

int a=1;
int b =7;
int c=b;
For(int i =1;i<=5;i++)
{
a=i;
For(int j=1;j<=I;j++)
{
System.out.print(a+" ");
a=a+b;
b--;
}
b=c;
}
System.out.println();
}

If you have any doubt please do comment.
Thank you.

Reply

Thanks Nitish ... for your solution
I also tried with another solution ::
please check if it is correct or not

for (int i = 1; i <=5; i++) {
int result =i;
for (int j=1;j<=i;j++) {
System.out.print(result+" ");
result= result+8-j;

}
System.out.println();
}

Reply
«Oldest   ‹Older   1201 – 1230 of 1230   Newer›   Newest»

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.