×
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
5624    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.
Albert Einstein
Do not worry about your difficulties in Mathematics. I can assure you mine are still greater.
Albert Einstein
1802
66.09
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     48308
25/06/2018     40333
01/01/2018     39789
28/06/2017     37665
02/08/2017     36401
01/08/2017     30651
06/07/2017     30397
15/05/2017     29849
14/07/2017     25832
11/09/2018     25306
softetechnologies