Showing posts with label Patterns. Show all posts
Showing posts with label Patterns. Show all posts

Thursday, 23 June 2016

Program to print a Star Pattern Type-7.

Explanation :
If n=6 then output will be
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern7
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j;
        for (i=0; i<line; i++)
        {
            for (j=0; j<line; j++)
            {
                System.out.printf(" * ");
            }
            System.out.printf("\n");
        }
    }
}

Program to print a Star Pattern Type-6.

Explanation :
If n=6 then output will be
 *
 *  *
 *  *  *
 *  *  *  *
 *  *  *  *  *
 *  *  *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern6
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=0; i<line; i++)
        {
            for (j=0; j<=i; j++)
            {
                System.out.printf(" * ");
            }
            System.out.printf("\n");
        }
    }
}

Program to print a Star Pattern Type-5.

Explanation :
If n=6 then output will be
           *
         **
       ***
     ****
   *****
 ******  .

Code:

import java.util.Scanner;
public class StarPattern5
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0,samp=1;
        for (i=1; i<=line; i++)
       {
            for (j=line; j>=i; j--)
           {
                System.out.printf(" ");
            }
            for (k=1; k<=i; k++)
           {
                System.out.printf("*");
            }
            System.out.printf("\n");
        }
    }
}
 

Program to print a Star Pattern Type-4.

Explanation :
If n=6 then output will be
  ******
    *****
      ****
        ***
          **
            *.

Code:

import java.util.Scanner;
public class StarPattern4
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0,samp=1;
        for (i=line; i>=1; i--)
        {
            for (k=samp; k>=0; k--)
            {
                System.out.printf(" ");
               
            }
            for (j=i; j>=1; j--)
            {
                System.out.printf("*");
            }
            samp = samp + 1;
            System.out.printf("\n");
        }
    }
 }

Program to print a Star Pattern Type-3.

Explanation :
If n=6 then output will be
 *  *  *  *  *  *
 *  *  *  *  *
 *  *  *  *
 *  *  *
 *  *
 * .

Code:

import java.util.Scanner;
public class StarPattern3
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=line; i>=1; i--)
        {
            for (j=1; j<=i; j++)
            {
                System.out.printf(" * ");
            }
            System.out.printf("\n");
        }
    }
 }

Program to print a Star Pattern Type-2.

Explanation :
If n=4 then output will be
       *
     *  *
   *  *  *
 *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern2
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=1; i<=line; i++)
        {
            for (k=t; k<line; k++)
            {
                System.out.printf(" ");
            }
            for (j=0; j< i; j++)
            {
                System.out.printf(" * ");
                t = t + 1;
            }
            System.out.printf("\n");
        }
    }
 }

Program to print a Star Pattern Type-1.

Explanation :
If n=6 then output will be
           *
         **
       ***
     ****
   *****
 ******
   *****
     ****
       ***
         **
           *.

Code:

import java.util.Scanner;
public class StarPattern1
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,samp=1;
        for (i=1; i<=line; i++)
        {
            for (k=samp; k<=line; k++)
            {
                System.out.printf(" ");
            }
            for (j=0; j< i; j++)
            {
                System.out.printf("*");
            }
            samp = samp + 1;
            System.out.printf("\n");
        }
        samp = 1;
        for (i=line-1; i>=1; i--)
        {
            for (k=samp; k>=0; k--)
            {
                System.out.printf(" ");
            }
            for (j=i; j>=1; j--)
            {
                System.out.printf("*");
            }
            samp = samp + 1;
            System.out.printf("\n");
        }
    }
}

Wednesday, 22 June 2016

Program to print a Number Pattern Type-28(Pascal Triangle).

Explanation :
If n =6 then output will be
              1
            1  1
          1  2  1
        1  3  3  1
     1  4   6   4  1
    1 5 10 10  5  1.

Code:

import java.util.Scanner;
public class Pattern28
{
    public static void main( String args[])
    {
        int line,i,j;
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the no. of lines: ");
        line=sc.nextInt();
       
    
        for(i=0;i<line;i++){
             for(j=0;j<line-i-1;j++)
                 System.out.printf(" ");
    
             for(j=0;j<=i;j++)
                 System.out.print(fact(i)/(fact(j)*fact(i-j)));
             System.out.printf("\n");
        }
    }
    
   public static long fact(int num){
        long f=1;
        int i=1;
        while(i<=num){
             f=f*i;
             i++;
      }
      return f;
     }
}

Program to print a Number Pattern Type-27.

Explanation :
If n=6 then output will be
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36.

Code:

import java.util.Scanner;
public class Pattern27
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d ",i*j);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-26.

If n=5 then output will be
11111
0000
111
00
1 .

Code:

import java.util.Scanner;
public class Pattern26
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i, j;
        for (i=n;i>=1;i--)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d",i%2);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-25.

Explanation :
If n=5 then output will be
1
10
101
1010
10101.

Code:

import java.util.Scanner;
public class Pattern25
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d",j%2);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-24.

Explanation :
If n=5 then output will be
                       1
                   2  3
               4  5  6
          7  8  9 10
 11 12 13 14 15 .

Code:

import java.util.Scanner;
public class Pattern24
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        k=1;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=1;j--)
            {
                if(j > i)
                        System.out.printf("   ");
                else
                        System.out.printf("%3d",k++);
            }
            System.out.printf("\n");
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-23.

Explanation :
If n=5 then output will be
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15 .

Code:

import java.util.Scanner;
public class Pattern23
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        k=1;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=i;j--)
            {
                System.out.printf("%3d",k++);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-22.

Explanation :
If n=5 then output will be
 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15  .

Code:

import java.util.Scanner;
public class Pattern22
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=1;
         for(int i = 1 ; i <= n ; i++)
         {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(" "+k++);
             }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-21.

Explanation :
If n=6 then output will be
            1
          123
        12345
      1234567
    123456789
1234567891011
    123456789
     1234567
       12345
         123
           1  .

Code:

import java.util.Scanner;
public class Pattern21
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i, j, k;
        for (i=1;i<=n;i++)
        {
            for (j=i;j<n;j++)
            {
                System.out.printf(" ");
            }
            for (k=1;k<(i*2);k++)
            {
                System.out.printf("%d",k);
            }
            System.out.printf("\n");
        }
        for (i=n-1;i>=1;i--)
        {
            for (j=n;j>i;j--)
            {
                System.out.printf(" ");
            }
            for (k=1;k<(i*2);k++)
            {
                System.out.printf("%d",k);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-20.

Explanation :
If n=6 then output will be
                         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
 1  2  3  4  5  6  7  8  9  10  11  .

Code:

import java.util.Scanner;
public class Pattern20
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1; i<=n; i++)
        {
            for (j=1; j<=n-i; j++)
            {
                System.out.printf("   ");
            }
            for (k=1; k<=2*i-1; k++)
            {
                System.out.printf(" %d ",k);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-19.

Explanation :
If n=6 then output will be
          1
        1 2
      1 2 3
    1 2 3 4
  1 2 3 4 5
1 2 3 4 5 6 .

Code:

import java.util.Scanner;
public class Pattern19
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>i;j--)
            {
                System.out.printf(" ");
            }
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d ",j);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-18.

Explanation :
If n=6 then output will be
        1
      2 2
     3 3 3
    4 4 4 4
   5 5 5 5 5
 6 6 6 6 6 6 .

Code:

import java.util.Scanner;
public class Pattern12
{
    public static void main(String args[])
    {
        System.out.print("Enter the number of Rows:");
        Scanner sc=new Scanner(System.in);
        int z=sc.nextInt();
        int i, k, x,j=5;
        for (i=1;i<=z;i++)
        {
            for (k=1;k<=j;k++)
            {
                System.out.printf(" ");
            }
            for (x=1;x<=i;x++)
            {
                System.out.print(i);
                System.out.printf(" ");
            }
            System.out.printf("\n");
            j=j-1;
        }
    }
}

Program to print a Number Pattern Type-17.

Explanation :
If n=6 then output will be
111111
22222
3333
444
55
6.

Code:

import java.util.Scanner;
public class Pattern17
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i, j;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=i;j--)
            {
                System.out.printf("%d",i);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-16.

Explanation :
If n=6 then output will be
1
22
333
4444
55555
666666.

Code:

import java.util.Scanner;
public class Pattern16
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
        sc.close();
    }
}