Java Examples : Patterns Part 3
softetechnologies
12-07-2017    1509 times
Java Patterns 03

Some another 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

java pattern

import java.util.*;
public class Pat17
{
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<=2*r-1;c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

java pattern

import java.util.*;
public class Pat18
{
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=1;
for(s=1;s<=n-r;s++)
{

Private Teacher

System.out.print(" ");
}
for(c=1;c<=2*r-1;c++)
{
System.out.print("" + p);
if(c<r)
{
p++;
}
else
{
p--;
}
}
System.out.println("");
}
}
}

java pattern

import java.util.*;
public class Pat19
{
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<=2*r-1;c++)
{
System.out.print("" + p);
if(c<r)
{
p++;
}
else
{
p--;
}
}
System.out.println("");
}
}
}

java pattern

import java.util.*;
public class Pat20
{
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();

Tutorial at home
Tutorial at home

for(r=1;r<=n;r++)
{
p=1;
for(s=1;s<=n-r;s++)
{
System.out.print(" ");
}
for(c=1;c<=2*r-1;c++)
{
System.out.print("" + p);
p=(p==0)?1:0;
}
System.out.println("");
}
}
}

java pattern

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

java pattern

import java.util.*;
public class Pat22
{
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<=r-1;s++)
{
System.out.print(" ");
}

Tutorial at home
Private Teacher

for(c=1;c<=2*(n-r)+1;c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

java pattern

import java.util.*;
public class Pat23
{
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=1;
for(s=1;s<=r-1;s++)
{
System.out.print(" ");
}
for(c=1;c<=2*(n-r)+1;c++)
{
System.out.print(""+p);
if(c<n-r+1)
{
p++;
}
else
{
p--;
}
}
System.out.println("");
}
}
}

java pattern

import java.util.*;
public class Pat24
{
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=n-r+1;
for(s=1;s<=r-1;s++)
{
System.out.print(" ");
}
for(c=1;c<=2*(n-r)+1;c++)
{
System.out.print(""+p);
if(c<n-r+1)
{
p--;
}
else
{
p++;
}
}
System.out.println("");
}
}
}

java pattern

import java.util.*;
public class Pat25
{
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<=2*n-1;r++)
{
for(s=1;s<=Math.abs(n-r);s++)
{
System.out.print(" ");
}
p=n-Math.abs(n-r);
for(c=1;c<=2*p-1;c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

java pattern

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

java pattern

import java.util.*;
public class Pat27
{
public static void main(String []args)
{
int n,c,r,s,p,pr;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Line no : ");
n=sc.nextInt();
for(r=1;r<=2*n-1;r++)
{
pr=1;
if(r==n || r==n+1)
continue;
for(s=1;s<=Math.abs(n-r);s++)
{
System.out.print("*");
}
p=n-Math.abs(n-r);
for(c=1;c<=2*p-1;c++)
{
System.out.print(" ");
}
for(s=1;s<=Math.abs(n-r);s++)
{
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