Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: To find the possible consecutive natural no.s which when added, give user-entered no.

1. ## To find the possible consecutive natural no.s which when added, give user-entered no.

The question states : "Write a program which inputs a positive natural number N and prints the possible consecutive number combinations, which when added give N".
For example :
N=9
Output :
2 3 4
4 5

Another Example:
N=27
Output:
2 3 4 5 6 7
8 9 10
13 14

Here's my source code :
import java.io.*;
class neutral
{
int N;
void main()throws IOException
{
System.out.println("Enter a Number:");
int arr[]=new int[N];
int no=1;
int f=0;
int r=f;
int sum=0;
for(int i=0; i<N; i++)
{
arr[i]=no;
no++;
}
for(f=0; f<N; f++)
{
for(r=f; r<N; r++)
{
sum=sum+arr[r];
if(sum==N)
{
break;
}
}
if(sum==N)
{
break;
}
}
for(int k=f; k<r; k++)
{
System.out.println(arr[k]+" ");
}
}
}

=========================
It accepts the number but gives no output.
Can you please verify the errors, correct it, and tell me where I went wrong.
Thanks. =)

2. ## Re: To find the possible consecutive natural no.s which when added, give user-entered

Originally Posted by amandeshmukh5
Can you please verify the errors, correct it, and tell me where I went wrong.
Thanks. =)
That's not how this work. I recommend you read through this: How to Program (stuck on homework? READ THIS BEFORE POSTING) as well as the link in my signature on asking questions the smart way.

Also, you should try stepping through this with a debugger, or at least adding some more print statements, to figure out what's going on in your program.