Solve any number patterns programs in Java
- Categories Java, Iterative Statements, Nested loops
Watch our video on How to solve any number pattern program in Java – Click Here
1. Write a python program to print following number pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1, p=1; i<=n; i++, p++){
for(int j=1; j<=i; j++){
System.out.print(p+" ");
}
System.out.println();
}
}
}
2. Write a python program to print following pattern:
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1, p=5; i<=n; i++, p++){
for(int j=1; j<=i; j++){
System.out.print(p+" ");
}
System.out.println();
}
}
}
3. Write a python code to print following number pattern:
0
2 2
4 4 4
6 6 6 6
8 8 8 8 8
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1, p=5; i<=n; i++, p+=2){
for(int j=1; j<=i; j++){
System.out.print(p+" ");
}
System.out.println();
}
}
}
4. Write a python code to print following pattern:
1
2 2
1 1 1
2 2 2 2
1 1 1 1 1
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
if(i%2==0)
System.out.print("2 ");
else
System.out.print("1 ");
}
System.out.println();
}
}
}
5. Write a python program to print following Dimond number pattern:
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6
7 7 7 7 7
8 8 8
9
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n= 5,p=1;
for(int i=1; i<n; i++, p++){
for(int j=i; j<=n; j++){
System.out.print(" ");
}
for(int j=1; j<i; j++){
System.out.print(p +" ");
}
for(int j=1; j<=i; j++){
System.out.print(p +" ");
}
System.out.println();
}
for(int i=1; i<=n; i++, p++){
for(int j=1; j<=i; j++){
System.out.print(" ");
}
for(int j=i; j<n; j++){
System.out.print(p +" ");
}
for(int j=i; j<=n; j++){
System.out.print(p +" ");
}
System.out.println();
}
}
}
6. Write a python program to print following diamond number pattern:
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4
3 3 3 3 3
2 2 2
1
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n= 5;
for(int i=1, p= 1; i<=n; i++, p++){
for(int j=i; j<=n; j++){
System.out.print(" ");
}
for(int j=1; j<i; j++){
System.out.print(p +" ");
}
for(int j=1; j<=i; j++){
System.out.print(p +" ");
}
System.out.println();
}
for(int i=1, p=5; i<=n; i++, p--){
for(int j=1; j<=i; j++){
System.out.print(" ");
}
for(int j=i; j<n; j++){
System.out.print(p +" ");
}
for(int j=i; j<=n; j++){
System.out.print(p +" ");
}
System.out.println();
}
}
}
7. Write a python program to print number pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1; i<=n; i++){
int p=1;
for(int j=1; j<=i; j++){
System.out.print(p++ +" ");
}
System.out.println();
}
}
}
8. Write a python code to print following number pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n= 5;
for(int i=1; i<=n; i++){
int p =1;
for(int j=1; j<=i; j++){
System.out.print(" ");
}
for(int j=i; j<=n; j++){
System.out.print(p++ +" ");
}
System.out.println();
}
}
}
9. Write a python code to print following number hill pattern:
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n= 5;
for(int i=1; i<=n; i++){
int p =1;
for(int j=i; j<=n; j++){
System.out.print(" ");
}
for(int j=1; j<i; j++){
System.out.print(p++ +" ");
}
for(int j=1; j<=i; j++){
System.out.print(p++ +" ");
}
System.out.println();
}
}
}
10. Write a python code to print following number pattern :
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1; i<=n; i++){
int p=5;
for(int j=1; j<=i; j++){
System.out.print(p-- +" ");
}
System.out.println();
}
}
}
11. Write a python code to print following number pattern :
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=5;
for(int i=1,k=n; i<=n;i++,k--){
int p=k;
for(int j=1; j<=i; j++){
System.out.print(" ");
}
for(int j=i; j<=n; j++){
System.out.print(p-- +" ");
}
System.out.println();
}
}
}
12. Write a python code to print hill pattern with numbers :
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
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n= 5;
for(int i=1; i<=n; i++){
int p =1;
for(int j=i; j<=n; j++){
System.out.print(" ");
}
for(int j=1; j<i; j++){
System.out.print(p++ +" ");
}
for(int j=1; j<=i; j++){
System.out.print(p-- +" ");
}
System.out.println();
}
}
}
13. Write a python code to print following number pattern :
1
2 3
4 5 6
7 8 9 10
Code:
public class CharacterPattern
{
public static void main(String[] args)
{
int n=4, p=1;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
System.out.print(p++" ");
}
System.out.println();
}
}
}