Skip to main content

Print all possible combinations of given sum from 1 to n

Here given code implementation process.

//C Program
//Print all possible combinations of given sum from 1 to n
#include<stdio.h>

//Print all possible combinations of numbers to reach a given sum
void combinations(int n ,int result[] ,int index,int sum)
{
  
  if(index > n || sum >n) 
  {
    return;
  }
  
  if(sum == n)
  { 
    //Display result
    for (int i = 0; i < index; ++i)
    {
      printf(" %d ",result[i] );
    }

    printf("\n");
  }
  else
  {
    for (int i = n; i >= 1; --i)
    {
      //get the number
      result[index] = i;
      combinations(n ,result,index+1,sum + i);
    }
   
  }
}

int main()
{
 
 
  int n=6;

  //auxiliary space to store result of given sum
  int result[n];

  combinations(n,result,0,0);


  return 0;
}

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
#include<iostream>

using namespace std;

/*
  C++ Program
  Print all possible combinations of given sum from 1 to n
*/
class MyArray {
	public:

		//Print all possible combinations of numbers to reach a given sum
		void combinations(int n, int result[], int index, int sum) {
			if (index > n || sum > n) {
				return;
			}
			if (sum == n) {
				//Display result

				for (int i = 0; i < index; ++i) {
					cout << " " << result[i] << " ";
				}
				cout << "\n";
			} else {
				for (int i = n; i >= 1; --i) {
					//get the number
					result[index] = i;
					this->combinations(n, result, index + 1, sum + i);
				}
			}
		}
};
int main() {
	MyArray obj = MyArray();
	int n = 6;
	//auxiliary space to store result of given sum
	int result[n];
	obj.combinations(n, result, 0, 0);
	return 0;
}

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
/*
  Java Program
  Print all possible combinations of given sum from 1 to n
*/

public class MyArray 
{
  //Print all possible combinations of numbers to reach a given sum
  public void combinations(int n ,int []result ,int index,int sum)
  {
    
    if(index > n || sum >n) 
    {
      return;
    }
    
    if(sum == n)
    { 
      //Display result
      for (int i = 0; i < index; ++i)
      {
        System.out.print(" "+result[i]+" ");
      }

      System.out.print("\n");
    }
    else
    {
      for (int i = n; i >= 1; --i)
      {
        //get the number
        result[index] = i;
        combinations(n ,result,index+1,sum + i);
      }
     
    }
  }

  public static void main(String[] args) {

    MyArray obj = new MyArray();
    
    int n=6;

    //auxiliary space to store result of given sum
    int []result = new int[n];

    obj.combinations(n,result,0,0);
 
  }
}

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
/*
  C# Program
  Print all possible combinations of given sum from 1 to n
*/
using System;

public class MyArray {
	//Print all possible combinations of numbers to reach a given sum
	public void combinations(int n, int[] result, int index, int sum) {
		if (index > n || sum > n) {
			return;
		}
		if (sum == n) {
			//Display result

			for (int i = 0; i < index; ++i) {
				Console.Write(" " + result[i] + " ");
			}
			Console.Write("\n");
		} else {
			for (int i = n; i >= 1; --i) {
				//get the number
				result[index] = i;
				combinations(n, result, index + 1, sum + i);
			}
		}
	}
	public static void Main(String[] args) {
		MyArray obj = new MyArray();
		int n = 6;
		int[]
		//auxiliary space to store result of given sum
		result = new int[n];
		obj.combinations(n, result, 0, 0);
	}
}

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
<?php
/*
  Php Program
  Print all possible combinations of given sum from 1 to n
*/
class MyArray {
	//Print all possible combinations of numbers to reach a given sum

	public 	function combinations($n, &$result, $index, $sum) {
		if ($index > $n || $sum > $n) {
			return;
		}
		if ($sum == $n) {
			//Display result

			for ($i = 0; $i < $index; ++$i) {
				echo(" ". $result[$i] ." ");
			}
			echo("\n");
		} else {
			for ($i = $n; $i >= 1; --$i) {
				//get the number
				$result[$index] = $i;
				$this->combinations($n, $result, $index + 1, $sum + $i);
			}
		}
	}
}

function main() {
	$obj = new MyArray();
	$n = 6;
	//auxiliary space to store result of given sum
	$result = array_fill(0, $n, 0);
	$obj->combinations($n, $result, 0, 0);

}
main();

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
/*
  Node Js Program
  Print all possible combinations of given sum from 1 to n
*/
class MyArray {
	//Print all possible combinations of numbers to reach a given sum
	combinations(n, result, index, sum) {
		if (index > n || sum > n) {
			return;
		}

		if (sum == n) {
			//Display result

			for (var i = 0; i < index; ++i) {
				process.stdout.write(" " + result[i] + " ");
			}

			process.stdout.write("\n");
		} else {
			for (var i = n; i >= 1; --i) {
				//get the number
				result[index] = i;
				this.combinations(n, result, index + 1, sum + i);
			}
		}
	}
}

function main(args) {
	var obj = new MyArray();
	var n = 6;
	//auxiliary space to store result of given sum
	var result = Array(n).fill(0);
	obj.combinations(n, result, 0, 0);
}

main();

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
# Python 3 Program
# Print all possible combinations of given sum from 1 to n
class MyArray :
	# Print all possible combinations of numbers to reach a given sum
	def combinations(self, n, result, index, sum) :
		if (index > n or sum > n) :
			return
		
		if (sum == n) :
			# Display result
			i = 0
			while (i < index) :
				print(" ", result[i] ," ", end = "")
				i += 1
			
			print("\n", end = "")
		else :
			i = n
			while (i >= 1) :
				# get the number
				result[index] = i
				self.combinations(n, result, index + 1, sum + i)
				i -= 1
			
		
	

def main() :
	obj = MyArray()
	n = 6
	result = [0] * n
	obj.combinations(n, result, 0, 0)


if __name__ == "__main__":
	main()

Output

  6
  5    1
  4    2
  4    1    1
  3    3
  3    2    1
  3    1    2
  3    1    1    1
  2    4
  2    3    1
  2    2    2
  2    2    1    1
  2    1    3
  2    1    2    1
  2    1    1    2
  2    1    1    1    1
  1    5
  1    4    1
  1    3    2
  1    3    1    1
  1    2    3
  1    2    2    1
  1    2    1    2
  1    2    1    1    1
  1    1    4
  1    1    3    1
  1    1    2    2
  1    1    2    1    1
  1    1    1    3
  1    1    1    2    1
  1    1    1    1    2
  1    1    1    1    1    1
# Ruby Program
# Print all possible combinations of given sum from 1 to n
class MyArray 
	# Print all possible combinations of numbers to reach a given sum
	def combinations(n, result, index, sum) 
		if (index > n || sum > n) 
			return
		end
		if (sum == n) 
			# Display result
			i = 0
			while (i < index) 
				print(" ", result[i] ," ")
				i += 1
			end
			print("\n")
		else 
			i = n
			while (i >= 1) 
				# get the number
				result[index] = i
				self.combinations(n, result, index + 1, sum + i)
				i -= 1
			end
		end
	end
end
def main() 
	obj = MyArray.new()
	n = 6
	result = Array.new(n, 0)
	obj.combinations(n, result, 0, 0)
end
main()

Output

 6 
 5  1 
 4  2 
 4  1  1 
 3  3 
 3  2  1 
 3  1  2 
 3  1  1  1 
 2  4 
 2  3  1 
 2  2  2 
 2  2  1  1 
 2  1  3 
 2  1  2  1 
 2  1  1  2 
 2  1  1  1  1 
 1  5 
 1  4  1 
 1  3  2 
 1  3  1  1 
 1  2  3 
 1  2  2  1 
 1  2  1  2 
 1  2  1  1  1 
 1  1  4 
 1  1  3  1 
 1  1  2  2 
 1  1  2  1  1 
 1  1  1  3 
 1  1  1  2  1 
 1  1  1  1  2 
 1  1  1  1  1  1 
/*
  Scala Program
  Print all possible combinations of given sum from 1 to n
*/
class MyArray {
	//Print all possible combinations of numbers to reach a given sum
	def combinations(n: Int, result: Array[Int], index: Int, sum: Int): Unit = {
		if (index > n || sum > n) {
			return;
		}
      	var i: Int = 0;
		if (sum == n) {
			//Display result
			i = 0;
			while (i < index) {
				print(" " + result(i) + " ");
				i += 1;
			}
			print("\n");
		} else {
			i = n;
			while (i >= 1) {
				//get the number
				result(index) = i;
				this.combinations(n, result, index + 1, sum + i);
				i -= 1;
			}
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyArray = new MyArray();
		val n: Int = 6;
		var result: Array[Int] = Array.fill[Int](n)(0);
		obj.combinations(n, result, 0, 0);
	}
}

Output

 6
 5  1
 4  2
 4  1  1
 3  3
 3  2  1
 3  1  2
 3  1  1  1
 2  4
 2  3  1
 2  2  2
 2  2  1  1
 2  1  3
 2  1  2  1
 2  1  1  2
 2  1  1  1  1
 1  5
 1  4  1
 1  3  2
 1  3  1  1
 1  2  3
 1  2  2  1
 1  2  1  2
 1  2  1  1  1
 1  1  4
 1  1  3  1
 1  1  2  2
 1  1  2  1  1
 1  1  1  3
 1  1  1  2  1
 1  1  1  1  2
 1  1  1  1  1  1
/*
  Swift Program
  Print all possible combinations of given sum from 1 to n
*/
class MyArray {
	//Print all possible combinations of numbers to reach a given sum
	func combinations(_ n: Int, _ result: inout [Int], _ index: Int, _ sum: Int) {
		if (index > n || sum > n) {
			return;
		}
      	var i: Int = 0;
		if (sum == n) {
			//Display result
			i = 0;
			while (i < index) {
				print(" ", result[i] ," ", terminator: "");
				i += 1;
			}
			print("\n", terminator: "");
		} else {
			i = n;
			while (i >= 1) {
				//get the number
				result[index] = i;
				self.combinations(n, &result, index + 1, sum + i);
				i -= 1;
			}
		}
	}
}
func main() {
	let obj: MyArray = MyArray();
	let n: Int = 6;
	var result: [Int] = Array(repeating: 0, count: n);
	obj.combinations(n, &result, 0, 0);
}
main();

Output

  6
  5    1
  4    2
  4    1    1
  3    3
  3    2    1
  3    1    2
  3    1    1    1
  2    4
  2    3    1
  2    2    2
  2    2    1    1
  2    1    3
  2    1    2    1
  2    1    1    2
  2    1    1    1    1
  1    5
  1    4    1
  1    3    2
  1    3    1    1
  1    2    3
  1    2    2    1
  1    2    1    2
  1    2    1    1    1
  1    1    4
  1    1    3    1
  1    1    2    2
  1    1    2    1    1
  1    1    1    3
  1    1    1    2    1
  1    1    1    1    2
  1    1    1    1    1    1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment