0% found this document useful (0 votes)
32 views3 pages

Subset Program

Uploaded by

Shambhavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Subset Program

Uploaded by

Shambhavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Subset program

Design and implement C/C++ Program to find a subset of a given set S = {sl ,
s2,.....,sn} of n positive integers whose sum is equal to a given positive integer d.
#include<stdio.h>

int subset(int,int);

int s[10],d,n,set[10],count=0;

void display(int);

int flag = 0;

void main()

int i;

printf("ENTER THE NUMBER OF THE ELEMENTS IN THE SET : ");

scanf("%d",&n);

printf("ENTER THE SET OF VALUES : ");

for(i=0;i<n;i++)

scanf("%d",&s[i]);

printf("ENTER THE SUM : ");

scanf("%d",&d);

printf("THE PROGRAM OUTPUT IS: ");

subset(0,0);

if(flag == 0)

printf("There is no solution");

}
int subset(int sum,int i)

if(sum == d)

flag = 1;

display(count);

return 1;

if(sum>d || i>=n)return 1;

else

set[count]=s[i];

count++;

subset(sum+s[i],i+1);

count--;

subset(sum,i+1);

void display(int count)

int i;

printf("\t{");

for(i=0;i<count;i++)

printf("%d,",set[i]);
printf("}");

Output:

ENTER THE NUMBER OF THE ELEMENTS IN THE SET : 5

ENTER THE SET OF VALUES : 6

ENTER THE SUM : 5

THE PROGRAM OUTPUT IS: ​ {4,1,}​ {3,2,}

You might also like