You are on page 1of 2

Discrete structures and Logic Lab(KCS-353)

ASSESSMENT

AIM: To find the power set of a set

DESCRIPTION: Power set of a set is a set of all the subset of a set , for example let S={a,b}
then power set of s , P(s) = {{},{a},{b},{a,b}}

CODE:

#include <stdio.h>
#include <math.h>
void display(char *set, int set_size)
{
int counter, j;
int powset_size = pow(2, set_size);
printf("Power set is\n");
printf("{");
printf("' '");
for(counter = 0; counter < powset_size; counter++)
{
for(j = 0; j < set_size; j++)
{
if(counter & (1<<j))
printf("%c ", set[j]);
}
if(counter!=powset_size-1)
printf(",");
}
printf("}");
}

int main()
{
char set[50];
int n,i;
printf("Enter how many elements you want in set ");
scanf("%d",&n);
if(n==0)
{
printf("Power set is\n");
printf("{ }");
}
else
{
printf("Enter the elements ");
for(i=0;i<n;i++)
{
scanf("%s",&set[i]);

DIVAKAR 2100910130045 IT1 A(2)


Discrete structures and Logic Lab(KCS-353)

}
display(set, n);
}
return 0;
}

OUTPUT:

Enter how many elements you want in set 3


Enter the elements
3
4
5
Power set is
{' ',3 ,4 ,3 4 ,5 ,3 5 ,4 5 ,3 4 5 }

Enter how many elements you want in set 4


Enter the elements
2
4
7
9
Power set is
{' ',2 ,4 ,2 4 ,7 ,2 7 ,4 7 ,2 4 7 ,9 ,2 9 ,4 9 ,2 4 9 ,7 9 ,2 7 9 ,4 7 9 ,2 4 7 9 }

DIVAKAR 2100910130045 IT1 A(2)

You might also like