Thursday , 21 February 2019
Home » Class 12 » Program to print the possible consecutive number combinations

Program to print the possible consecutive number combinations

Question:

A positive natural number, (for e.g. 27), can be represented as follows:
2+3+4+5+6+7
8+9+10
13+14
where 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.

SAMPLE DATA

INPUT:
N = 9

OUTPUT:
4 + 5
2 + 3+ 4

INPUT:
N = 15

OUTPUT:
7 +8
1 +2+ 3+ 4+ 5
4 +5+ 6

INPUT:
N = 21

OUTPUT:
10+ 11
1+ 2+ 3+ 4+ 5+ 6
6+ 7+ 8

Programming Code:

/**
* The class ISC06PQ1 inputs a number and prints all the series of consecutive natural numbers whose
sum equals the number
* @author : www.javaforschool.com
* @Program Type : BlueJ Program – Java
* @Question Year : ISC Practical 2006 Question 1
*/

import java.io.*;
class ISC06PQ1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter a number : “); //inputting the number
int n=Integer.parseInt(br.readLine());

int sum=0,c=0,j=0;
for(int i=1;i

Output:

1. Enter a number : 15

1 + 2 + 3 + 4 + 5
4 + 5 + 6
7 + 8

2. Enter a number : 95

5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14
17 + 18 + 19 + 20 + 21
47 + 48

Check Also

Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.

5 comments

  1. Sir is it ok if we convert a number into a string and then perform the tasks required in the practicals? are marks cut for that?

  2. THANK YOU SO MUCH SIR/MAM …..

  3. sir/mam please upload the code for keith number please!!!!!!!!!!

Leave a Reply to JAY BHATT Cancel reply

Your email address will not be published. Required fields are marked *