Java Examples : Patterns Part 1
softetechnologies
10-07-2017    1074 times
Java Patterns 01

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

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

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("");
}
}
}

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

Private Teacher

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("");
}
}
}

Tutorial at home
Tutorial at home

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("");
}
}
}

Tutorial at home
Private Teacher

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("");
}
}
}

softetechnologies
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.
    Related Post
  Query About the post