
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all possible sums of consecutive numbers with sum N in C++
In this problem, we are given a positive integer N and we have to print the sequence of all possible consecutive numbers with a sum equal to N.
Let’s take an example to understand the problem,
Input: N = 15 Output: 1 2 3 4 5 7 8
A simple solution to this problem is by adding up consecutive sequence combinations till N/2. And then print the sequence that sums up to N.
Example
#include<iostream> using namespace std; void printConsequtiveSum(int N){ int start = 1, end = (N+1)/2; while (start < end){ int sum = 0; for (int i = start; i <= end; i++){ sum = sum + i; if (sum == N){ for (int j = start; j <= i; j++) cout<<j<<" "; cout<<endl; break; } if (sum > N) break; } sum = 0; start++; } } int main(){ int N = 25; cout<<"Sequence of consicutive numbers that sum upto "<<N<<" are :\n"; printConsequtiveSum(N); return 0; }
Output
The sequence of consecutive numbers that sum up to 25 are −
3 4 5 6 7 12 13
This method is easy but is not so efficient.
So, we have a more complex but optimum solution that will be using a precomputed array of sums to keep track of sum. This will decrease the complexity of the sum.
Example
#include <iostream> using namespace std; void printConsequtiveSum(int N){ int start = 1, end = 1; int sum = 1; while (start <= N/2){ if (sum < N){ end += 1; sum += end; } else if (sum > N){ sum -= start; start += 1; } else if (sum == N){ for (int i = start; i <= end; ++i) cout<<i<<" "; cout<<endl; sum -= start; start += 1; } } } int main(){ int N = 25; cout<<"Sequence of consicutive numbers that sum upto "<<N<<" are:\n"; printConsequtiveSum(N); return 0; }
Output
Sequence of consecutive numbers that sum up to 25 are −
3 4 5 6 7 12 13
- Related Articles
- All possible binary numbers of length n with equal sum in both halves?
- Sum of square-sums of first n natural numbers
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Finding all possible combined (plus and minus) sums of n arguments JavaScript
- Print all sequences starting with n and consecutive difference limited to k in C++
- Possible two sets from first N natural numbers difference of sums as D in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Sum of consecutive numbers in JavaScript
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Consecutive Numbers Sum in C++
- Python program for sum of consecutive numbers with overlapping in lists
- Print all n-digit strictly increasing numbers in C++
- Print all valid words that are possible using Characters of Array in C++\n
- Express 18 as the sum of two prime numbers in all possible ways.
- Print all pairs with given sum in C++

Advertisements