- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given an integer n as input. The goal is to find the number of ways in which we can represent ‘num’ as the sum of two or more consecutive natural numbers. For example, if n is 3 it can be represented as sum ( 1+2 ) so total 1 way.

**For Example**

num=6

Count of ways to express a number as sum of consecutive numbers are: 1

The ways in which we can express ‘num’ as sum of consecutive natural numbers: 1+2+3

num=19

Count of ways to express a number as sum of consecutive numbers are: 1

The ways in which we can express ‘num’ as sum of consecutive natural numbers: 9+10

**Approach used in the below program is as follows** −

In this approach we will represent the number as the sum of ( a + a+1 + a+2…..+ a+i ).

Which becomes (a)(L+1) times + 1+2+3+4…+i = a*(i+1) + i*(i+1)/2. (sum of i natural numbers) num=a*(i+1) + i*(i+1)/2.a= [ num − (i)*(i+1)/2 ] / (i+1)

We will do this for i=1 to i*(i+1)/2 is less than num.

Take an integer num as input.

Function sum_consecutive(int num) takes a num and returns the count of ways to express ‘num’ as sum of consecutive natural numbers.

Take the initial count as 0.

Take temporary variable res as float.

Using for loop traverse from i=1 to i*(i+1)/2 < num.

Calculate the value [ num − (i)*(i+1)/2 ] / (i+1) and store in res.

If res is integer ( res − (int)res is 0 ) then increment count.

At the end we have count as ways in which num can be represented as the sum of consecutive natural numbers.

Return count as result.

#include <bits/stdc++.h> using namespace std; int sum_consecutive(int num){ int count = 0; int temp = num * 2; float res; for (int i = 1; i * (i + 1) < temp; i++){ int store = i + 1; res = (1.0 * num−(i * (i + 1)) / 2) / store; float check = res − (int)res; if(check == 0.0){ count++; } } return count; } int main(){ int num = 20; cout<<"Count of ways to express a number as sum of consecutive numbers are: "<<sum_consecutive(num) << endl; return 0; }

If we run the above code it will generate the following output −

Count of ways to express a number as sum of consecutive numbers are: 1

- Related Questions & Answers
- Count ways to express a number as sum of powers in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- Express an odd number as sum of prime numbers in C++
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- Minimum number of palindromes required to express N as a sum using C++.
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Minimum numbers needed to express every integer below N as a sum in C++
- C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
- Consecutive Numbers Sum in C++
- Program to count number of consecutive lists whose sum is n in C++
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Sum of consecutive numbers in JavaScript
- Count number of ways to divide a number in parts in C++
- Check if a number can be written as sum of three consecutive integers in C++
- Represent a Number as Sum of Minimum Possible Pseudo-Binary Numbers in C++

Advertisements