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<stdio.h>
#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);
primefactor(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;
}
else
ifprime = 1;
i++;
}
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)
{
prime(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;
}
else
{
factor++; //this cannot be made a part of the for loop
}
}
}
if (factor<=num){
primefactor();
else{
break;
}
return (0);
}