hi im told to make a program where the user enters a large number like 13195 and the prgrams gives it the highest prime factor, which i 29. now i have to do this using recursion in basic c, without using loops of any kind, so noo for, while etc. prof showed a way to call the funtion again by it self but dont really get him so thats why need a bit of help..
long int num;
long int prime(long int num);
long int primefactor(long int num);
printf("Enter the number whose prime factors are to be calculated:");
//The following is the function that detects a Prime number.
long int prime(long int num)
long int i, ifprime;
i = 2;
if (i <= num )
if (num % i == 0)
ifprime = 0;
ifprime = 1;
} //The following function prints the prime factors of a number.
long int primefactor(long int num)
long int factor, ifprime = 0;
factor = 2;
if (factor <= num)
//so that the factors are only prime and nothing else.
if (num % factor == 0) //diving by all the prime numbers less than the number itself.
printf("%ld ", factor);
num = num / factor;
factor++; //this cannot be made a part of the for loop
1 Year Ago
Related Article:find out prime number
is a C discussion thread by virendra_sharma that has 4 replies and was last updated 2 years ago.
Line 31 -- i is generally a loop counter variable. There is no loop (as per your instructions), but there is also no recursion. Why does the "prime" function return a long int rather than a bool, particularly since it can only return a 0 or a 1?
Line 47 -- You call "prime", then do absolutely nothing with the return value.
Ditto line 67 -- why bother having a function return a value if you do nothing with it?
Line 69 -- break? The "break" statement is designed to break you out of a loop. The instructions say no loops and your program has none. "break" therefore makes no sense.