Tuesday, June 16, 2026
HomeData Modelling & AIC++ Program for Ways to sum to N using Natural Numbers up...

C++ Program for Ways to sum to N using Natural Numbers up to K with repetitions allowed

Given two integers N and K, the task is to find the total number of ways of representing N as the sum of positive integers in the range [1, K], where each integer can be chosen multiple times.

Examples:

Input: N = 8, K = 2
Output: 5
Explanation: All possible ways of representing N as sum of positive integers less than or equal to K are:

  1. {1, 1, 1, 1, 1, 1, 1, 1}, the sum is 8.
  2. {2, 1, 1, 1, 1, 1, 1}, the sum is 8.
  3. {2, 2, 1, 1, 1, 1}, the sum is 8.
  4. 2, 2, 2, 1, 1}, the sum is 8.
  5. {2, 2, 2, 2}}, the sum is 8.

Therefore, the total number of ways is 5.

Input: N = 2, K = 2
Output: 2

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS