fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void printSeq(int num, int a[], int opIndx,int s){
  6. if(num <= 0){
  7. //Display the output
  8. for(int j = 0; j < opIndx; j++)
  9. cout << a[j] << ",";
  10. cout << endl;
  11. return;
  12. }
  13.  
  14. // s starts from 1 and later it will change to
  15. // 2, 3, 4, 5.
  16. // It is the case$
  17. // 5 = 2 + (3)
  18. // 5 = 3 + (2)
  19. // 5 = 4 + (1)
  20. // 5 = 5 + (0)
  21. for(int i = s; i <= num; i++){
  22. a[opIndx] = i;
  23. printSeq(num-i, a, opIndx + 1, i);
  24. }
  25. }
  26.  
  27. int main(){
  28. int a[15];
  29. printSeq(5, a, 0, 1);
  30. return 0;
  31. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
1,1,1,1,1,
1,1,1,2,
1,1,3,
1,2,2,
1,4,
2,3,
5,