Results 1 to 5 of 5
  1. #1
    maniku is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Help needed with a Java exercise

    Hi all,

    I'm taking a java programming course, and can't figure out an exercise. Could someone help?

    The exercise is to create a programme which asks the user to give a value for n and then calculate the sum of:

    1 + 2 + 3 + ... + n

    (e.g. if n = 6, it would be 1 + 2 + 3 + 4 + 5 + 6)

    But: you have to do it WITHOUT using a loop.

    I can't get further than:

    XML Code:
    import java.util.*;
    
    public class T27_Summa1_2_3_n {
    
        private static Scanner input = new Scanner(System.in);
    
        public static void main(String[] args) {
            System.out.print("Anna n: ");
            int n = Integer.parseInt(input.nextLine());
        }
    }
    Probably some obvious solution which I'm totally missing... Thanks!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try using a recursive method.

    I'll quickly explain what recrsion is but I don't want to ruin it for you so I'll save examples for if you get really stuck.
    A recursive method generally is broken down into it's parts, one part is a termination clause, what should occur for the method to finally return? The other part is a recursive call. To keep it simple let's work with a recursive process. The method should do something then call itself.


    Here is a good recursive definition, I'll leave the translation and base case to you

    To find the sum of numbers up to n you want to:
    Add n to sum(n - 1)

    Java Code:
    public int sumN(int n){
      if condition is met
        return base case
      else
        do something and make a recursive call
    }
    A recursive call is a method calling itself. Recursion is a bit strange coming from usig loops so it may take a bit to sink on, however; it's definitely nice to understand.

  3. #3
    maniku is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Solved:

    XML Code:
    import java.util.*;
    
    public class T27_Summa1_2_3_n {
    
        private static Scanner input = new Scanner(System.in);
    
        public static int summa(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n + summa(n - 1);
        }
    }
        
        public static void main(String[] args) {
            System.out.print("Anna n: ");
            int n = Integer.parseInt(input.nextLine());
            System.out.println("Tulos: " + summa(n));
        }
    }
    Thank you for your pointers, sunde887!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top of the page.

  5. #5
    maniku is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    There seems to be a problem with that:

    Content Encoding Error

    The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

Similar Threads

  1. Java exercise I REALLY need help on
    By velvetymold in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 02:02 AM
  2. finishing up a DB/Java exercise
    By hayden06f4i in forum New To Java
    Replies: 47
    Last Post: 12-12-2010, 09:57 PM
  3. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-13-2010, 11:14 PM
  4. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-13-2010, 11:13 PM
  5. Help needed with Java exercise - Including arrays - Reward
    By TheDarkReverend in forum New To Java
    Replies: 7
    Last Post: 10-23-2008, 02:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •