Q.A partition of a positive integer n is a sequence of positive integers that sum to n. Write a program to print all non-increasing partitions of n.
eg. n=4
4
3 1
2 2
2 1 1
1 1 1 1
Code:
#include<stdio.h>
void printint(int, int);
int main(void)
{
int num;
printf("Enter number: ");
scanf("%d", &num);
printint(num, 0);
return 0;
}
void printint(int n, int a)
{
int j ,k;
j = n-a;
k = n-j;
printf("%d%d\n", j, k);
a++;
if(k>1)
{
printf("%d", j);
printint(k, k-1);
}
if(j!=1)
printint(n,a);
}
My code doesnt work for number more than 3! Any idea? Is there any other way to solve this problem? In need of help! This is not a homework question! Thanks!