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
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter a Number:");
N=Integer.parseInt(br.readLine());
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. =)
Re: To find the possible consecutive natural no.s which when added, give user-entered
Quote:
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: http://www.javaprogrammingforums.com...e-posting.html 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.