Need help with recusive function

This is a discussion on Need help with recusive function within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I need to write a recursive function that accepts a positive integer, n , and calculates the following ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Need help with recusive function

    Hi everyone, I need to write a recursive function that accepts a positive integer, n , and calculates the following sum of integers between 3 and n, inclusive, that are divisible by 3. ex. 3 + 6 + 9....

    Im away from a compiler so I cant test it but so far I have...
    Code:
    int n;
    int temp;
    int recur(n)
    {
        if (n <= 1 ) return 1;
        temp =  n + recur(n-3);
        return temp;
    }
    
    void main()
    {
        cin >> n;
        n = n - (n % 3);
        recur(n);
    }
    Am I on the right track?

    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You're on the right track. There's a few issues you'll find when you test it with a compiler, but the basic idea looks right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21