## Getting factors using recursive method

prime factors of a number using recursion in java
c program to find factors of a number using recursion
algorithm to find factorial of a number using recursion
using recursion write a program that returns the value of n
calculating factorial with recursion
factorial using recursion in java
recursively find factors
how to find prime factors of a large number in c

The output was correct but it keeps on displaying `-1` at the end of the factors from time to time (infinite looping). I know there's something wrong with the `while` loop inside the recursion method but I don't know what it is. How can I stop the recursive call after getting the final answer? What should I use instead of a `while` loop?

```public static void main() {
int num;
System.out.print("\fInput:");
num = kb.nextInt();
int temp = 1;
factors(num, temp);
}

public static void factors(int num, int temp) {
int count = 2;

while (temp != num) {
if ((num % count) == 0) {
System.out.print(count + " * ");
factors(num / count, temp * count);
} else
count++;
}
}
```

First, you don't need a recursion here. If you use recursion, you will always start new with `count = 2`, even if you already checked bigger values.

```factors(num/count, temp*count);
```

I would just use

```num = num/count;
```

Second, as Attila already said, you don't need the temp parameter and the real check you need is whether `num != 1` is true.

If you really want to use recursion, there would be a better way: Pass the method `factors` the counter so that you don't always have to start with 2. In this case, you don't need any loop:

```public static void main()
{
int num;
System.out.print("\fInput:");
num=kb.nextInt();
int count=2;
factors(num, count);

}

public static void factors(int num, int count)
{
if (num == 1)
return;

if((num%count)==0)
{
System.out.print(count+ " * ");
factors(num/count, count);
}
else
factors(num,count+1);
}
}
```

Getting factors using recursive method, An alternative (and probably more efficient) approach would be to derive all prime factors (hint: you could use divmod ) and then create each of the possible  You can often use simple transforms from the looping form to the recursive form. Local variables must generally be moved into a parameter. There is often two forms, one providing the user interface and another, often private, that actually performs the recursive function.

Your stop condition (temp!=num) will never be false. When you get the final factor and you'd like to stop, you will call factors(1, "the original number"), so the while loop will go on.

```while (num != 1)
```

And the temp parameter is not needed.

Getting the unique factors of a number recursively, Let Us C by BPB Publications https://amzn.to/2oeEhXP C: The Complete Reference by McGraw Duration: 3:02 Posted: May 8, 2018 Since, it is called from the same function, it is a recursive call. In each recursive call, the value of argument num is decreased by 1 until num reaches less than 1. When the value of num is less than 1, there is no recursive call. And each recursive calls returns giving us: 6 * 5 * 4 * 3 * 2 * 1 * 1 (for 0) = 720

As other's pointed out, `while (temp!=num)` will never be `false` and the program will continue looping endlessly. Karl_Costa's solution works well, however, you can still use your while loop for counting purposes as shown. And as other's pointed out, there's no need for using `temp`.

Here's another working solution-

```public static void main() {
int num;
System.out.print("\fInput:");
num=kb.nextInt();
System.out.print("1");  // To always print 1 as a factor.
factors(num, 2);
}

public static void factors(int num, int count) {
while(num%count != 0 && num>count) {
count++;
}
System.out.print(" * " + count);
if(num==count) {
return;
}
if(num%count==0)
factors(num/count, count);
else {
factors(num/count, count+1);
}
}
```

C Program to Print Prime Factors of a Number Using Recursion , Modify the function suitably to obtain the prime factors recursively. find prime factors of Duration: 20:58 Posted: May 12, 2020 It's entirely acceptable to mix loops with recursive techniques. In the case of finding prime factors, it makes perfect sense to use a recursive call every time a new factor is identified. The stack will only ever get as deep as the number of factors, plus one or two. No problem - even really big numbers don't have that many factors.

```class RecursiveFactors
{
static void factors(int a, int n)
{
if(n%a == 0)
{
System.out.println(a);
}

if(a<=n)
factors(a+1, n);
}

public static void main(String args[])
{
factors(1,25);
}
}
```

C Program To Find Prime Factors of a Number using Recursion , In this C programming example, you will learn to find the factorial of a non-​negative integer entered by the user using recursion. Then, 5 is passed to multiplyNumbers() from the same function (recursive call). In each recursive call, the value of argument n is C Example. Display Factors of a Number · C Example. The method CalculateSumRecursively is our recursive method that calculates the sum of the numbers from n to m. The first thing we do is to set our sum to the value of n. Then, we check if the value of n is less than the value of m. If it is, we increase the value of n by 1 and add to our sum a result of the same method but with the increased n.

C Program to Find Factorial of a Number Using Recursion, In this program, you'll learn to display all factors of a given number using for loop in Java. Here’s a Simple Program to find factorial of a number using both recursive and iterative methods in C Programming Language. This Program prompts user for entering any integer number, finds the factorial of input number and displays the output on screen.

Java Program to Display Factors of a Number, java program to find factorial of a given number using recursion. By Chaitanya Singh nextInt(); //Called the user defined function fact int factorial = fact(num);  The factorial() method is calling itself. Initially, the value of n is 4 inside factorial(). During the next recursive call, 3 is passed to the factorial() method. This process continues until n is equal to 0. When n is equal to 0, the if statement returns false hence 1 is returned. Finally, the accumulated result is passed to the main() method.

java program to find factorial of a given number using recursion, In the case of finding prime factors, it makes perfect sense to use a recursive call every time a new factor is identified. The stack will only ever get  Java Program to Find Factors of a Number Using Functions. This Java program allows the user to enter any positive integer value. Then, we are going to pass the User entered value to the Method we created. Within this User defined function, this program will find Factors of a number using the Java For Loop