Program for Sum of Subset Problem in C

Program

#include <stdio.h>
#include <conio.h>
void subset(int, int, int);
int x[10], w[10], d, count = 0;
void main()
{
    int i, n, sum = 0;
    printf("\n*********************************************************");
    printf("\n*********************************************************");
    printf("\n**      Program for Sum of Subset Problem in C         **");
    printf("\n**                Created by Sheetal Garg              **");
    printf("\n**                 Assistant Professor                 **");
    printf("\n**                 Phone No:9467863365                 **");
    printf("\n*********************************************************");
    printf("\n*********************************************************");
    printf("\nEnter the no. of elements: ");
    scanf("%d", &n);
    printf("\nEnter the elements in ascending order:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &w[i]);
    printf("\nEnter the sum: ");
    scanf("%d", &d);
    for (i = 0; i < n; i++)
        sum = sum + w[i];
    if (sum < d)
    {
        printf("No solution\n");
        getch();
        return;
    }
    subset(0, 0, sum);
    if (count == 0)
    {
        printf("No solution\n");
        getch();
        return;
    }
    getch();
}
void subset(int cs, int k, int r)
{
    int i;
    x[k] = 1;
    if (cs + w[k] == d)
    {
        printf("\n\nSubset %d\n", ++count);
        for (i = 0; i <= k; i++)
        {
            if (x[i] == 1)
                printf("%d\t", w[i]);
        }
    }
    else if (cs + w[k] + w[k + 1] <= d)
        subset(cs + w[k], k + 1, r - w[k]);
    if (cs + r - w[k] >= d && cs + w[k] <= d)
    {
        x[k] = 0;
        subset(cs, k + 1, r - w[k]);
    }
}

Output

*********************************************************
*********************************************************
**      Program for Sum of Subset Problem in C         **
**                Created by Sheetal Garg              **
**                 Assistant Professor                 **
**                 Phone No:9467863365                 **
*********************************************************
*********************************************************
Enter the no. of elements: 4

Enter the elements in ascending order:
3      
4
5
1

Enter the sum: 9


Subset 1
3       5       1

Subset 2
4       5
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.