Thread: Need help with recusive function

  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,366
    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, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM