edited by
2,605 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
edited by

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

0 votes
0 votes
0 answers
4
ne0n asked Apr 20, 2018
434 views
Hey Guys,How to prepare for the IITK programming test.what is the level of questions??Any sample questions from previous years?? Please guys any help wud be appreciated.T...