Factor
A factor is an integer that can be divided evenly into another number or in other words factors of a number are numbers that multiply to form a product.
Logic
To print all the factors of a particular number we have to iterate through all the smaller numbers up to the given number. If the user given number is completely divisible by any number then it is a factor of that number.
Program
import java.util.Scanner; public class AllFactorOfN { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Take input from user System.out.print("Enter any number : "); int num = scanner.nextInt(); System.out.println("\nAll factors of " + num + " are : "); for(int i = 1; i <= num; i++) { if(num % i == 0) { System.out.print(i + " "); } } } }
Output
Enter any number : 50
All factors of 50 are :
1 2 5 10 25 50