Thread: Recursive problem with my code

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    11

    Recursive problem with my code

    Code:
    #include<stdio.h>
    
    
    int main (void)
    {
        int number, checkFactor;
        printf ("Please enter a number between 2 and 1000000.\n\n");
        scanf ("%d", &number);
    
    
        if (number < 2 || number > 1000000)
        {
            printf ("\nThis number is not in the domain.\n\n");
            return 0;
        }
    
    
        checkFactor = primenesscheck (number);
    
    
        if (checkFactor == 1)
        {
                printf ("%d=", number);
                primefactorization (number);
        }
    
    
        return 0;
    }
    
    
    int primenesscheck(int number)
    {
        int i, checkPrime, notprime;
        for (i=2; i<number; i++)
        {
            if (number % i == 0)
            {
                number = notprime;
                break;
            }
        }
    
    
        if (number == notprime)
        {
            printf ("\nThis number is not prime.\n\n");
            checkPrime = 1;
        }
    
    
        else
        {
            printf ("\nThis number is prime.\n\n");
            checkPrime = 2;
        }
        return checkPrime;
    }
    
    
    void primefactorization (int number)
    {
        int currentfactor,checkingfactor,newnumber;
    
    
        for (currentfactor=2;currentfactor<=number;currentfactor++)
        {
            checkingfactor = number % currentfactor;
    
    
            if (checkingfactor == 0)
            {
                printf ("(%d)", currentfactor);
    
    
                newnumber = number/currentfactor;
    
    
                if (newnumber == 1)
                {
                    break;
                }
    
    
                primefactorization(newnumber);
            }
        }
    
    
        return 0;
    }
    So I'm suffering a problem in my code. When primefactorization is initialised to calculate the product of prime factors of the number (in ascending order), the first couple of values are what I need. However, the program then starts doing stuff that I don't want it to. For instance, if I write 49 as my number, instead of 49=(7)(7), I get 49=(7)(7)(49). Same with any other non prime like 100. Where is my error?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Your loop condition and exit conditions are wrong, the algorithm in general doesn't look right. Why don't you describe what your intent was.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    11
    My intent is to create a program that determines whether a number is prime or not, and if it is prime, then calculate the products of prime numbers that compose the non-prime number and list them in ascending order.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    When this line is executed
    primefactorization(newnumber);

    What happens after the function call returns?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    11
    The function is executed again but with a different number.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by tivanenk View Post
    The function is executed again but with a different number.
    Wrong, I suggest you review how recursion works or use a debugger or printf statements to understand the cause of your problem.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-08-2012, 12:54 AM
  2. Searching recursive(with code)
    By johny145 in forum Windows Programming
    Replies: 0
    Last Post: 06-16-2005, 11:08 AM
  3. [code] Recursive Search Function
    By anonytmouse in forum Windows Programming
    Replies: 1
    Last Post: 12-17-2004, 11:21 PM
  4. Recursive Function in Pseudo Code
    By smitsky in forum Tech Board
    Replies: 3
    Last Post: 10-24-2004, 10:17 AM
  5. Recursive code
    By Korhedron in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2003, 06:29 PM

Tags for this Thread