tags:

views:

151

answers:

3

i made a recursive function to find the prime factors of a number but it has a bug which makes turbo c quit. please help

#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
    int num;
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    primefactor(num,i);
     i=num 
    getch();
}
int primefactor(int num,int i)
{
    if(i==2)
    return 1;
    if(num%i==0)
    {
        if(prime(num))
        {
            printf(",%d",num);
            num=num/i;
            i++;
        }


    }
    i--;
    primefactor(num,i);
    return 0;
}
int prime(int num)
{
    int i,flag;
    for(i=2;i<num;i++)
    {
        if(num%i==0)
    flag=0;
    }

return flag; }

+4  A: 
void main(void)
{
    int num,i=num; // (*)
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    primefactor(num,i);
    getch();
}

What value do you think i will have in (*)?

Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:

void main(void)
{
    int num,i; 
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    i = num; // assignment goes here.
    primefactor(num,i);
    getch();
}
IVlad
oh lol so stupid of me :P thanks a lot :)
fahad
`void main`? =(
jamesdlin
i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better
fahad
@fahad it *has* to be int main(void) in your case (the other case is when you have command line params)educate your teachers with this: http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B
neal aise
A: 

Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?

Will A
...and - how does prime return it's value to the caller?
Will A
i used my concept of prime factor calculation and a prime factor program made without recursion for the help.http://pastebin.com/fVbjFGzQ
fahad
I don't see a return statement anywhere in your prime function.Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...
Will A
A: 

(little too sleepy to write good code.. so am sorry in advance for any bugs :p )

a simpler non recursive version

printPrimeFactors(int num) {

  for (i = 2; i < sqrt(num); i=getNextPrime()) {
     if (num %i)
        printf("%d", i);
  } 

}

if you have to use recursion

printPrimeFactors(int num) {

 if(isPrime(num)) {
    printf ("%d ", num);
 } else {
    for(i=2; i < sqrt(num); i++) {
        if(num%i ==0) {
              printPrimeFactors(i);
              printPrimeFactors(num/i);   
        }
    }
 }

}
neal aise
Can any1 explain that :(
fahad
will put in some explanation soon. sorry about that
neal aise