Showing posts with label pascal triangle. Show all posts
Showing posts with label pascal triangle. Show all posts

Thursday, 4 July 2013

Print Pascal Triangle

/**
 * @(#)PascalTriangle.java
 *
 *
 * @author -> Anant Mahale
 * @version 1.00 2013/7/3
 */
import java.io.*;
class PascalTriangle
{
    public static void main(String []args)
    {
        try
        {
                DataInputStream cin=new DataInputStream(System.in);
            System.out.print("Enter number of rows for pascal triangle:");
    
                  int n = Integer.parseInt(cin.readLine());
                  for (int y = 0; y < n; y++)
                  {
                        int c = 1;
                        for(int q = 0; q < n - y; q++)
                        {
                              System.out.print("   ");
                        }
                        for(int x = 0; x <= y; x++)
                        {
                              System.out.print("   ");
                              System.out.print(c); // 3 digits
                              System.out.print(" ");
                              c = c * (y - x) / (x + 1);
                        }
                        System.out.println();
                        System.out.println();
                  }   
                  System.out.println();
           
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Thursday, 13 June 2013

program for pascal triangle in java

// simple_paskal_trangle

import java.io.*;
class simple_pascal_triangle
{
    public static void main(String args[])
    {
        try
        {
            int j,k,i=1,no;
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("enter the number of lines of pascal triangle");
            no=Integer.parseInt(cin.readLine());

            for(j=1;j<=no;j++)
            {
                for(k=1;k<=j;k++)
                {
                    System.out.print(""+i);
                    System.out.print(" ");
                    i++;
                }
                System.out.println();
            }
        }
        catch(Exception e)
        {
            System.out.println("wrong data");
        }
    }
}