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

Wednesday, March 21, 2012

Java program to print a particular line of the Pascal's Triangle


/*This Java program accepts a number from the user and
 * then prints the particular line of the
 * pascal's triangle onto the screen. More information
 * about pascal's triangle: http://en.wikipedia.org/wiki/Pascal's_triangle
 Sample :
 Enter the line number : 6
 1.0 5.0 10.0 10.0 5.0 1.0
 */

import java.util.*;

public class pascal
{
    public static void main()
    {
         Scanner sc=new Scanner(System.in);
         System.out.print("Enter the line number : ");
         int l=sc.nextInt();
         double a[]=new double[l];
         a[0]=1;
         int j,k;
         for(j=1;j<l;j++)
         {
             a[j]=1;
             for(k=j-1;k>0;k--)
             {
                 a[k]=a[k]+a[k-1];
             }
             a[0]=1;
         }
         for(j=0;j<l;j++)
         {
             System.out.print(a[j]+" ");
         }
    }
}

//Author : Mayank Rajoria

Sunday, November 20, 2011

Printing a pyramid pattern of digits #12

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :6
Sample Output :
                0
             1 0 1
          2 1 0 1 2
       3 2 1 0 1 2 3
    4 3 2 1 0 1 2 3 4
 5 4 3 2 1 0 1 2 3 4 5   */

import java.util.*;
class pattern
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=(2*(n-i));j++)
        {
            System.out.print(" ");
        }
       
        for(j=i;j>=0;j--)
        {
            System.out.print(j+" ");
        }
       
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");
        }
        System.out.println();
    }
}
}

//Author : Mayank Rajoria

Thursday, October 27, 2011

Printing a pyramid pattern of digits #11

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :6
Sample Output :
1
22
333
4444
55555
666666   */


import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=1;
    for(i=1;i<=n;i++,k+=2)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(i);
        }
        System.out.println();
    }
}
}

//Author: Mayank Rajoria

Printing a dual pyramid pattern of '*' #4

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :7
Sample Output :

            *
          *A*
        *A*A*
      *A*A*A*
    *A*A*A*A*
  *A*A*A*A*A*
*A*A*A*A*A*A*   */



import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=1;
    for(i=1;i<=n;i++,k+=2)
    {
        for(j=n;j>i;j--)
        {
            System.out.print(" ");
        }
        for(j=1;j<=k;j++)
        {
            if(j%2==0)
            {
                System.out.print("A");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }
}
}

//Author: Mayank Rajoria

Printing a pyramid pattern of digits #10

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :7
Sample Output :

1234567
  123456
    12345
      1234
        123
          12
            1  */

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=1;
    for(i=n;i>=1;i--,k++)
    {
        for(j=1;j<k;j++)
        {
            System.out.print(" ");
        }
        for(j=1;j<=i;j++)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}

//Author : Mayank Rajoria

Tuesday, October 25, 2011

Printing a pyramid pattern of digits #9

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 7

Sample Output :
            7
          767
        76567
      7654567
    765434567
  76543234567
7654321234567 */
//For a left aligned version of this pattern
//visit: Java Programs: Printing a pyramid pattern of digits 8 (Java)

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=n,l=1;
    for(i=1;i<=n;i++)
    {
        for(j=n;j>=i+1;j--)
        {
            System.out.print(" ");
        }
        k=n;
        for(j=1;j<=i;j++,k--)
        {
            System.out.print(k);
        }
        k=k+2;
        for(j=1;j<=i-1;j++,k++)
        {
            System.out.print(k);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #8

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6

Sample Output :
6
656
65456
6543456
654323456
65432123456 */
//For a center aligned version of this pattern
//visit : Java Programs: Printing a pyramid pattern of digits 9 (Java)

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=n,l=1;
    for(i=1;i<=n;i++)
    {
        k=n;
        for(j=1;j<=i;j++,k--)
        {
            System.out.print(k);
        }
        k=k+2;
        for(j=1;j<=i-1;j++,k++)
        {
            System.out.print(k);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Monday, October 24, 2011

Printing a pyramid pattern of digits #7

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6

Sample Output :
          1
        121
      12321
    1234321
  123454321
12345654321 */

import java.util.*;
class pattern10

{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j,k;
    for(i=1;i<=n;i++)
    {
        for(k=i;k<n;k++)
        {
            System.out.print(" ");
        }
        for(j=1;j<=i;j++)
        {
            System.out.print(j);
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #6

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6
Sample Output :
               1
            1 2 1
         1 2 3 2 1
      1 2 3 4 3 2 1
   1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1  */

import java.util.*;
class pattern
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j,k;
    for(i=1;i<=n;i++)
    {
        for(k=i;k<n;k++)
        {
            System.out.print("  ");
        }
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j+" ");
        }
        System.out.println();
    }
}
}

//Author : Mayank Rajoria

Printing a pyramid pattern of digits #5

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6
Sample Output :
1
121
12321
1234321
123454321
12345654321 */

import java.util.*;
class pattern
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");  //If you want you may remove the '+" "' part.
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j+" "); //If you want you may remove the '+" "' part.
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Sunday, October 16, 2011

Printing a dual pyramid pattern of '*' #3

//This Java program accepts a number from the
//user and prints a dual pyramid using '*' depending
//on the number entered by the user
/* Sample Input :8
Sample Output :
* ******
** *****
*** ****
**** ***
***** **
****** *    */

import java.util.*;
class pattern7
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the size of pattern you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=(n-2);i++)
    {
        for(j=1;j<=(n);j++)
        {
            if(j==(i+1))
            {
                System.out.print(" ");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a dual pyramid pattern of '*' #2

//This Java program accepts a number from the
//user and prints a dual pyramid using '*' depending
//on the number entered by the user
/*Sample input :6
Sample Output :
  *****
* ****
** ***
*** **
**** *
*****  
*/


import java.util.*;
class pattern
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the size of pattern you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i==j)
            {
                System.out.print(" ");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #4

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input :4
Sample Output :
1
0 1
1 0 1
0 1 0 1 */

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,c=1,k=-1;
    int a=1,b=-1;
    for(i=1;i<=n;i++)
    {
        c=a;
        k=b;
        for(j=1;j<=i;j++)
        {
            System.out.print(c+" ");
            c=c+k;     
            k=k*(-1);  
        }
        System.out.println();
        a=a+b;     
        b=b*(-1);
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #3

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
//Sample Input :6
//Sample Output :
//1   2   3   4   5   6 
//7   8   9   10   11 
//12  13  14  15 
//16  17  18 
//19  20 
//21
import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int c=1;
    int i,j,k=1;
    for(i=n;i>=1;i--)
    {
        for(k=1;k<=i;k++)
        {
            System.out.print(c+"  ");
            c++;
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Tuesday, October 4, 2011

Printing a pyramid pattern of digits #1

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
//Sample Input :5
//Sample Output :
//1 
//2  3 
//4  5  6 
//7  8  9  10 
//11  12  13  14  15 
import java.util.*;
class pattern
{
public static void main()
{
    int i,j,k;
    System.out.println("Enter the number of lines you want");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    k=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(k+"  ");
            k++;
        }
        System.out.println();
    }
}
}
Author : Mayank Rajoria

Monday, September 26, 2011

Printing a pyramid pattern of digits #2

//This Java program prints the following pattern
//1
//11
//111
//1111
//11111
class pattern
{
public static void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(1);
}
System.out.println();
}
}
}
//Author : Mayank Rajoria

Sunday, September 25, 2011

Printing a dual pyramid pattern of '*' #1

//This Java program accepts a number from the
//user and prints a dual pyramid using 'a' of
//the number of layers entered by the user
//Sample input : 4
//Sample output :     *
//                           * *
//                          * * *
//                         * * * *
//                          * * *
//                           * *
//                            *

import java.util.*;
class pattern
{
public static void main(String Args[])
{
int c,k,n,x,l;
System.out.println("Enter a number");
Scanner ob=new Scanner(System.in);
x=ob.nextInt();
l=x;
for(n=1;n<=x;n++)
{
for(c=l;c>1;c--)
{
System.out.print(" ");
}
for(k=1;k<=n;k++)
{
System.out.print("* ");
}
System.out.println();
l--;
}

int y;
for(n=x;n>1;n--)
{
for(y=n;y<=x;y++)
{
System.out.print(" ");
}
for(k=1;k<n;k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
//Author : Mayank Rajoria