Friday, February 4, 2011

Finding All Possible Consecutive Number Combinations Using BlueJ Program

Amout the program on number of combination



We have to take a number and find out the sum of consecutive numbers within that entered number which is equal to the entered number itself. For example – if the entered number is 15 the consecutive number combinations will be
1 2 3 4 5
4 5 6
7 8 

Codes of the program on number combination

import java.io.*;
class NumberCombonation
{
int no,sum=0,k,j,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

 public void takeNumber()throws Exception
 {
System.out.println("Enter the number:");
no=Integer.parseInt(br.readLine());
 for(i=1;i<=no/2+1;i++)
 {
 sum=0;

  for(j=i;j<=no/2+1;j++)
  {
sum=sum+j;
if (sum==no)
break;
}
if(j<=no/2+1)
{
 for(k=i;k<=j;k++)
 System.out.print(" "+k);
 System.out.println();
}
}
}

public static void main(String args[])throws Exception
{
 NumberCombonation obj=new NumberCombonation ();
 obj.takeNumber();
}
}

Technical analysis of the number combination program

The outer loop starts from 1 and this loop is generates the starting number of consecutive number combination series and the first inner loop checks if the number combination is possible with that starting number. If a series is found, which is checked just outside the first inner loop, the second inner loop prints the consecutive number combination.



Related PostBlueJ Programs on Number

4 comments:

  1. the program on smith program is not running, so can u pls get me the ryt code....

    ReplyDelete
  2. I think you were getting some unwanted digits and numbers in the output. Those values were my checking points and now they are eliminated.

    ReplyDelete
  3. sir can u plz explain StringTokenizer in detail

    ReplyDelete
  4. Very soon I'll post on StringTokenizer class.

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner