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..

#include <math.h>
int main()
	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:");
	scanf("%ld", &num);


//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;


	return (ifprime);
}	//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 (ifprime)
			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	 


		if (factor<=num){

	return (0);
5 Years
Discussion Span
Last Post by VernonDozier

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.