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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. =)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by amandeshmukh5 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Natural language processing in java
    By nikki. in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 11:34 PM
  2. Image is not added when converting eml into htm
    By Vjay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2011, 06:24 AM
  3. Replies: 2
    Last Post: February 17th, 2011, 05:37 AM
  4. Replies: 1
    Last Post: December 4th, 2010, 04:26 PM
  5. Replies: 1
    Last Post: December 2nd, 2010, 04:29 PM

Tags for this Thread