Java Online Compiler
Example: Java Program to Find Factors of a Number using Recursion
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Java Program to Find Factors of a Number using Recursion import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("-----Enter the positive integer number-----"); int x = in.nextInt(); System.out.print("\nThe factors of the " + x + " are: "); FindFactors(x, 1); System.out.print("\n"); } // This function will return the factors of input number // Using the recursion public static void FindFactors(int x, int i) { // It will check if the number is less than if (i <= x) { if (x % i == 0) { System.out.print(i + " "); } // It will call the function recursively // To print the next number FindFactors(x, i + 1); } } }
90
Output
Clear
ADVERTISEMENTS