Thread: Recursive print every time...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    Recursive print every time...

    I am writing a recursive function to print all the divisor's of a number including 1 and itself.

    I am using a function
    Code:
    print_recur(int number)
    and inside that I am using
    Code:
    number % (number - 1)
    how can i get it to print out iff a number is divisible and just skip the ones that arent.

    It will end up printing for 10: 10, 5, 2, 1

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    Code:
    void print_all_divisor(int number) {
    	int divis;
    	if (number <=0) return 0;
    	if ((number%(number-1)) == 0)
    		{ divis = number-1;
    		printf("%d", divis);
    		return print_all_divisor(number-1);
    	}
    	if((number%(number-1)) != 0)
    		return print_all_divisor(number-1);
    When I do this it prints only the number 1, can anyone help w/ that?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    May be this will help you out..
    Code:
    #include <stdio.h>
    void divisor(int n,int i)
    {
     if(i<=n)
     {
       if(n&#37;i==0)
    		printf("%d ",n/i);
    	divisor(n,i+1);
     }
    }
    void print_recur(int n)
    {
      divisor(n,1);
    }
    
    int main()
    {
     int n=10;
     print_recur(n);
     return 0;
    }
    Last edited by chottachatri; 10-27-2008 at 12:34 AM.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Or even this one?

    Code:
    #include <stdio.h>
    void print_recur(int n)
    {
      static int i=1;
      if(i<=n)
      {
    	if(n&#37;i==0)
    		printf("%d ",n/i);
    	i++;
    	print_recur(n);
      }
      else
               i=1;
    
    }
    
    int main()
    {
     int n=10;
     print_recur(n);
     return 0;
    }
    Last edited by chottachatri; 10-27-2008 at 01:02 AM.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    No number will will ever be divided by its one-lesser other than 2 (1).
    Without using loops or statics, I don't think this can be done with only a single parameter recursion function. If you're implementing this for pedantic reasons, you may want to take a look at the gcd algo (Greatest Common Divisor) as its generally a good starting point for learning recursion.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    There you go Chotta man i owe you a big one thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM