Skip to main content

Union of Two Sets

// A Union B

#include <stdio.h>
#define SIZE 10

void display(int disp[])
{
    int i;
    for(i=0;disp[i];i++)
    {
        printf(" %d ", disp[i]);
    }
}
int main()
{
    int i,j,k=0, flag,elem1, elem2;
    int a[SIZE];
    int b[SIZE];
    int c[SIZE]={'\0'};
    char ch='y';
    printf("Enter the Elements of A : ");
    scanf("%d", &elem1);
    printf("Enter the value for A : ");

    for(i=0;i<elem1 && elem1 < SIZE;i++)
    {
        printf("a[%d] = ", i);
        scanf("%d",&a[i]);
    }
    a[i]='\0';
    printf("Enter the Elements of B : ");
    scanf("%d", &elem2);
    printf("Enter the Value for B: ");
    for(i=0;i<elem2 && elem2 <SIZE ;i++){
        printf("b[%d] = ", i);
        scanf("%d",&b[i]);
    }
    b[i]='\0';
    printf("\nSet - A = { ");
    display(a);
    printf(" } ");
    printf("\nSet - B = { ");
    display(b);
    printf(" } ");

    printf("\n A Union B = { ");
    for(k=0;a[k];k++)
    {
        c[k]=a[k];
    }
    for(i=0;i< elem2 ;i++)
    {
        flag=1;
        for(j=0;j< elem1 ;j++)
        {
            if(b[i]==a[j])
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
        {
            c[k]=b[i];
            k++;
        }
    }
    display(c);
    printf("} \n");
    return 0;
}



Comments

Popular posts from this blog

Dynamic Matrix Multiplication

// Matrix Multiplication #include<stdio.h> #define SIZE 5 int main() {     int a[SIZE][SIZE],b[SIZE][SIZE],c[SIZE][SIZE],d[SIZE][SIZE];     int i,j,k,r1,r2,c1,c2;     printf("Enter the Rows of Matrix - A : ");     scanf("%d",&r1);     printf("Enter the Columns of Matrix - A : ");     scanf("%d",&c1);     printf("Enter the Rows of Matrix - B : ");     scanf("%d",&r2);     printf("Enter the Columns of Matrix - B : ");     scanf("%d",&c2);     if(c1 == r2)     {         printf("Enter Elements of Matrix - A\n");         for(i=0;i<r1;i++)         {             for(j=0;j<c1;j++)             {                 printf("a[%d][%d] : ",i,j);       ...

Static Matrix Multiplication

// Matrix Multiplication #include<stdio.h> int main() {     int a[3][3],b[3][3],c[3][3],d[3][3];     int i,j,k;         printf("Enter Elements of Matrix - A\n");         for(i=0;i<3;i++)         {             for(j=0;j<3;j++)             {                 printf("a[%d][%d] : ",i,j);                 scanf("%d",&a[i][j]);             }         }         printf("Enter Elements of Matrix - B \n");         for(i=0;i<3;i++)         {             for(j=0;j<3;j++)             {                 printf("b[%d][%d] : ",i,j);  ...

Simple Matrix

// Simple Matrix Upto n Numbers. (Dynamic) #include<stdio.h> int main() {     int i,j,k=1,rc;     printf("Enter the No of Columns : ");     scanf("%d",&rc);     for(i=0;i<rc;i++)     {          printf("|");         for(j=0;j<rc;j++,k++)         {             printf("%3d",k);         }         printf(" | \n");     }     return 0; }