Thursday, 25 July 2013

Program for conversion of binary to decimal

import java.io.*;
class BintoDec
{
    public static void main(String args[])
    {
        int a,dec=0;
        try
        {
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter Binary Number to Convert");
            int bin=Integer.parseInt(cin.readLine());
            for(int i=0;bin>0;i++)
            {
                a=bin%10;
                dec=dec+a*((int)Math.pow(2,i));
                bin=bin/10;
            }
            System.out.println("Given Binary number conversion in Decimal is = "+dec);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

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