Let's learn java programming language with easy steps

Tuesday, 8 August 2017

Number Pattern Programs In Java


Java Number Pattern Programs

Number Pattern Programs

This article is all about of number pattern programs in java. This number pattern program is also a very important from the interview point of view.

Here we will see many java number pattern programs examples.

So let's start with it.

Number Pattern 1 :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Solution :

class Demo1
{
public static void main(String args[])
{
for(int i = 1; i<=5; i++)
{
for(int j = 1; j<=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 2 :

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Solution :


class Demo2
{
public static void main(String args[])
{
for(int i = 1; i<=5; i++)
{
for(int j = 1; j<=i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}

Number Pattern 3 :

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Solution :


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


Number Pattern 4 :

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Solution :


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

Here we are performing number pattern programs in java using for loop. In other words, here we are using nested loop i.e loop inside another loop.

Number Pattern 5 :

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Solution :


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

Number Pattern 6 :

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

Solution :


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

here another number pattern program in java which will help you in a java programming interviews.

Number Pattern 7 :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Solution :


class Demo7
{
public static void main(String args[])
{
//upper half of the pattern
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}

//lower half of the pattern
for(int i = 5-1; i >= 1; i--)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 8 :

8 7 6 5 4 3 2 1
8 7 6 5 4 3 2
8 7 6 5 4 3
8 7 6 5 4  
8 7 6 5
8 7
8

Solution :


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

Number Pattern 9 :

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
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
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8

Solution :


class Demo9
{
public static void main(String args[])
{
for(int i = 8; i >= 1; i--)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for(int i = 2; i <= 8; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 10 :

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1

Solution :


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

}
}

Here above all the java program to print of number are quite useful and all the above number pattern programs increase you logical capability.







Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Translate