Consecutive Number Sum Combinations Program in Java and Python
Consecutive number sum combinations program with algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
A positive natural number, for example 27, can be represented as follows:
Every row represents a combination of consecutive natural numbers which add up to 27.
Write a program which inputs a positive natural number N and prints the possible consecutive number combinations which, when added, give N.
Test your program for the following data and some random data.
Algorithm:
Step 1: Start.
Step 2: Accept a positive natural number n.
Step 3: Run an outer loop with starting number i from 1 to n - 1.
Step 4: For each value of i, set sum = i.
Step 5: Set j = i + 1 so that the next consecutive number can be added.
Step 6: While sum is less than n, add j to sum and then increase j by 1.
Step 7: When the while loop stops, check whether sum is equal to n.
Step 8: If sum == n, print all numbers from i to j - 1 as one consecutive combination.
Step 9: If sum is greater than n, ignore that starting number because it cannot form a valid combination.
Step 10: Repeat the process for all possible starting numbers.
Step 11: Stop.
Explanation:
The program searches for all groups of consecutive natural numbers whose sum is equal to the given number. A consecutive group means that every number comes immediately after the previous one, such as 4 + 5 + 6 or 10 + 11. The program does not know the first number of the group in advance, so it tries every possible starting number from 1 up to one less than the given number.
For each starting number i, the variable sum is first initialized with i. The variable j is then set to i + 1, which is the next consecutive natural number. The inner while loop keeps adding j to sum and then increases j. This simulates forming a sequence such as 4, then 4 + 5, then 4 + 5 + 6, and so on.
The inner loop continues only while sum is less than the target number. Once the sum becomes equal to or greater than n, there is no use adding more numbers for that same starting point. Since all numbers being added are positive, the sum can only increase. Therefore, if the sum has already crossed n, it can never come back down to become equal to n.
After the loop stops, the program checks sum == n. If this condition is true, the starting number i has produced a valid consecutive combination. At this moment j has already been increased once after adding the last number, so the actual combination runs from i to j - 1. A separate for loop prints these numbers with plus signs between them. If the sum is greater than n, nothing is printed for that starting value and the outer loop moves to the next possible starting number.
Java Program:
/**
* The class ISC06PQ1 inputs a number and prints all series of consecutive
* natural numbers whose sum is equal to that number.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2006 Question 1
*/
import java.util.Scanner;
class ISC06PQ1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
/*
* Each value of i is treated as the first number of a possible
* consecutive-number combination.
*/
for(int i = 1; i < n; i++)
{
int sum = i;
int j = i + 1;
/*
* Keep adding the next consecutive number until the sum becomes
* equal to or greater than the given number.
*/
while(sum < n)
{
sum = sum + j;
j++;
}
/*
* If the sum is exactly n, the numbers from i to j - 1 form
* one valid combination.
*/
if(sum == n)
{
for(int k = i; k < j; k++)
{
if(k == i)
System.out.print(k);
else
System.out.print(" + " + k);
}
System.out.println();
}
}
}
}Equivalent Python Program:
n = int(input("Enter a number: "))
# Each value of i is the first number of a possible sequence.
for i in range(1, n):
total = i
j = i + 1
# Add consecutive numbers until total reaches or crosses n.
while total < n:
total = total + j
j = j + 1
# If total is exactly n, print numbers from i to j - 1.
if total == n:
line = ""
for k in range(i, j):
if k == i:
line = line + str(k)
else:
line = line + " + " + str(k)
print(line)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.