10 November 2010

powerset program in java

The program was to find the power set of the given set . It was given to us as a lab work, i guess it might be useful for others as software community should always look for "REUSABILITY"



import java.io.*;

class powerset
{
   
    public static void main(String args[]) throws Exception
    {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   
       int i;char buff[]=new char[30];
       String str[]=new String[1024];
   
       str[0]="phi";
   
    System.out.print("Enter the no. of elements in the set : ");
    int x=Integer.parseInt(br.readLine());
    System.out.println();
    double n=Math.pow(2,x);
   
        for(i=0;i<x;i++)
        {
        System.out.print("Enter the "+(i+1)+" element of the set : ");
        String s=br.readLine();
        buff[i]=s.charAt(0);
        System.out.println();
        }
   
       
    for(i=1;i<n;i++)
    {
       
    str[i]=Integer.toString(i,2);
    int z=str[i].length();
    for(int j=0;j<x-z;j++)
    str[i]="0"+str[i];       
       
    }
       
        System.out.println("THE POWER SET IS : ");   
        System.out.println(str[0]);
       
        for(i=1;i<n;i++)
        {
            for(int j=0;j<x;j++)
            {
            char chk=str[i].charAt(j);
            if(chk=='1')
            System.out.print(buff[j]);
               
               
            }
           
        System.out.println();
           
           
           
           
      }
       
       
       
       
       
       
       
       
       
       
        }
   
   
    }



Delicious add to del.icio.us saved by 0 users

2 comments:

  1. THIS PROGRAM IS NOT WORKING AT ALL!!!IT IS SHOWING SOME ERRORS!!!!!!!

    ReplyDelete
  2. i dont knw wht u r doing..bt its working fine here...

    first the prog asks for d no. of elements in d set..suppose your input is 3

    then it will ask to input those 3 elements...suppose u type a,b,c

    then its showing the powerset....

    ReplyDelete