Union of 2 sets,A and B(2)Intersection of 2 sets,A and B.(3)Show that operation A-B and B-A are always false for sets.
21---Program to perform.
(1)Union of 2 sets,A and B.
(2)Intersection of 2 sets,A and B.
(3)Show that operation A-B and B-A are always false for sets.
import java.util.*;
class U_I_M
{
private int l1,l2;
public void intersection(int a[],int b[]){
int i,j,k=0,l3;
l1=a.length;
l2=b.length;
l3=Math.min(l1,l2);
int c[]=new int[l3];
for(i=0;i<l1;i++){
for(j=0;j<l2;j++){
if(a[i]==b[j]){
c[k++]=a[i];
break;
}
}
}
System.out.println("Intersection of two sets is:");
System.out.print("{");
for(i=0;i<k;i++){
if(i!=k-1)
System.out.print(c[i]+",");
else
System.out.print(c[i]+"}");
}
}
public void union(int a[],int b[]){
boolean f;
int i,j,k,l;
l1=a.length;
l2=b.length;
int s[]=new int[l1+l2];
k=l1;
for(i=0;i<l1;i++)
s[i]=a[i];
for(i=0;i<l2;i++){
f=true;
for(j=0;j<k;j++){
if(b[i]==s[j]){
f=false;}
}
if(f)
s[k++]=b[i];
}
System.out.println("Union of two sets is:");
System.out.print("{");
for(i=0;i<k;i++){
if(i!=k-1)
System.out.print(s[i]+",");
else
System.out.print(s[i]+"}");
}
}
}
OUTPUT---
When Input Is {1,3,5,7,6,4} & {2,3,4}
Intersection of two sets is:
{3,4}
Union of two sets is:
{1,3,5,7,6,4,2}
(1)Union of 2 sets,A and B.
(2)Intersection of 2 sets,A and B.
(3)Show that operation A-B and B-A are always false for sets.
import java.util.*;
class U_I_M
{
private int l1,l2;
public void intersection(int a[],int b[]){
int i,j,k=0,l3;
l1=a.length;
l2=b.length;
l3=Math.min(l1,l2);
int c[]=new int[l3];
for(i=0;i<l1;i++){
for(j=0;j<l2;j++){
if(a[i]==b[j]){
c[k++]=a[i];
break;
}
}
}
System.out.println("Intersection of two sets is:");
System.out.print("{");
for(i=0;i<k;i++){
if(i!=k-1)
System.out.print(c[i]+",");
else
System.out.print(c[i]+"}");
}
}
public void union(int a[],int b[]){
boolean f;
int i,j,k,l;
l1=a.length;
l2=b.length;
int s[]=new int[l1+l2];
k=l1;
for(i=0;i<l1;i++)
s[i]=a[i];
for(i=0;i<l2;i++){
f=true;
for(j=0;j<k;j++){
if(b[i]==s[j]){
f=false;}
}
if(f)
s[k++]=b[i];
}
System.out.println("Union of two sets is:");
System.out.print("{");
for(i=0;i<k;i++){
if(i!=k-1)
System.out.print(s[i]+",");
else
System.out.print(s[i]+"}");
}
}
}
OUTPUT---
When Input Is {1,3,5,7,6,4} & {2,3,4}
Intersection of two sets is:
{3,4}
Union of two sets is:
{1,3,5,7,6,4,2}
Comments
Post a Comment