in Programming in C edited by
2,578 views
4 votes
4 votes
C Program to find addition factors of a number in lexicographic order for n < 15. For example if n = 4 then output will be :
1 1 1 1
1 1 2
2 2
1 3
in Programming in C edited by
2.6k views

1 Answer

0 votes
0 votes
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>


char str[100] = {0};

int partition(int i, int n, int strpos) {

    if(n < 0) return 0;
    
    if(strpos >= 0) str[strpos] = i+48;
    if(n == 0) {
        if(strpos == 1) return 1;
        str[strpos] = 0;
        printf("%s\n", str);
        return 1;
    }
    
    if(i > n) return 0;
    
    return partition(i, n-i, strpos+1) + partition(i+1, n, strpos);
}


int main() {
    
    int n;
    scanf("%d", &n);
    partition(1, n, 0);
    return 0;
}

Related questions