+ 2
Please write a program to show all subsets of set of first n number given by user
2 Réponses
+ 2
Here is the program:-
#include <iostream>
using namespace std;
int main()
{
int i;
int j;
int x;
int a[4]={1,2,3,4};
/*The below loop prints the subsets having one term*/
for (i = 0; i<=3; i++)
{
cout << a[i] <<"\n";
}
/*The below loop prints the subsets having 2 terms*/
for (i = 0; i<=3; i++)
{
for (j=i+1; j<=3; j++)
{
cout << a[i] << ", "<<a[j] <<"\n";
}
}
/* The below loop prints the subsets having 3 terms */
for (i=0; i<=1; i++)
{
for (j=i+2; j<=3;j++)
{
cout<< a[i] << ", " << a[i+1]<< ", " << a[j]<<endl;
if((j==3) &&((i+2)!=3))
{
for(x=i+2;x!=3; x++)
{
cout<< a[i] << ", " << a[x]<< ", " << a[j]<<endl;
}
}
}
}
/*This below code lines print the subset having 4 terms*/
cout<< "1, 2, 3, 4"<<endl;
return (0);
}
+ 1
I'll try to solve this problem. I think you can solve with recursive function.