×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java Examples : Number Series Java Examples : Patterns Part 2
Java Examples : Patterns Part 1 - Java
5857    Arnab De    10/07/2017

Some basic program which are print the different parttern. Here we use the "for loop" for the same. Students are request to understand how the minimum change of a program can convert into another pattern. At first assume a matrix box and put the items on it. For more pattern see the following page

*
**
***
****
*****

softetechnologies

import java.util.*;
public class Pat1
{
public static void main(String []args)
{
int n,c,r;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

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


import java.util.*;
public class Pat2
{
public static void main(String []args)
{
int n,c,r;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
for(c=1;c<r;c++)
{
System.out.print("" + c);
}
System.out.println("");
}
}
}

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

import java.util.*;
public class Pat3
{
public static void main(String []args)
{
int n,c,r;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
{
System.out.print("" + r);
}
System.out.println("");
}
}
}

*
**
***
****
*****

softetechnologies

import java.util.*;
public class Pat4
{
public static void main(String []args)
{
int n,c,r,s;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=0;r<n;r++)
{
for(s=0;s<=n-r;s++)
{
System.out.print(" ");
}
for(c=0;c<=r;c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

1
22
333
4444
55555
666666

import java.util.*;
public class Pat5
{
public static void main(String []args)
{
int n,c,r,s;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
for(s=1;s<=n-r;s++)
{
System.out.print(" ");
}
for(c=1;c<=r;c++)
{
System.out.print("" + r);
}
System.out.println("");
}
}
}

softetechnologies

1
12
123
1234
12345

import java.util.*;
public class Pat6
{
public static void main(String []args)
{
int n,c,r,s;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
for(s=1;s<=n-r;s++)
{
System.out.print(" ");
}
for(c=1;c<=r;c++)
{
System.out.print("" + c);
}
System.out.println("");
}
}
}

softetechnologies

1
21
321
4321
54321

import java.util.*;
public class Pat7
{
public static void main(String []args)
{
int n,c,r,s,p;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
p=r;
for(s=1;s<=n-r;s++)
{
System.out.print(" ");
}
for(c=1;c<=r;c++)
{
System.out.print(""+p);
p--;
}
System.out.println("");
}
}
}

*
* *
* * *
* * * *
* * * * *
 * * * * * *

import java.util.*;
public class Pat8
{
public static void main(String []args)
{
int n,c,r,s;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=n;r++)
{
for(s=1;s<=n-r;s++)
{
System.out.print(" ");
}
for(c=1;c<=r;c++)
{
System.out.print("* ");
}
System.out.println("");
}
}
}

Java Examples : Number Series Java Examples : Patterns Part 2
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
All religions are true. God can be reached by different religions. Many rivers flow by many ways but they fall into the sea. They all are one.
Sri Sri Ramakrishna Paramahamsa
2490
71.27
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     51713
25/06/2018     43591
01/01/2018     42732
28/06/2017     40453
02/08/2017     39256
01/08/2017     33501
06/07/2017     33224
15/05/2017     32613
14/07/2017     28820
11/09/2018     28584
softetechnologies