Java Program to Find All Factors of a Number

  • Java program to print all factors of a number using loop and using a function.

Given a number N, we have ti print all factors of N using a java program. Before jumping into java program, here is the brief overview of factors of a number. A number F is a factor of number N, if and only if F divides N completely without leaving any remainder(N % F = 0). For example, 3 is a factor of 24 because 3 divides 24 without leaving any remainder.

24 / 3 = 8. and 24 % 3 = 0;
Here is the list of all factors of 24 : 1 2 3 4 6 8 12 24

Algorithm to find all factors of an integer
Let N be the given number. Check with every number from 1 to N, whether it divides N completely or not. Let, i be any number between 1 to N.
  • If(N % i == 0), then i is a factor of N.
  • If(N % i != 0), then i is not a factor of N.

Java program to print all factors of a number using loop

In this java program, we first take a number N as input from user and then print all factors of a N by implementing above mentioned algorithm using a for loop.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to print factors of a number
 */
public class PrintFactors {
    public static void main(String[] args) {
        int N, i;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter an Integer");
        N = scanner.nextInt();

        System.out.format("Factors of %d\n", N);
        /*
         * Check for every number between 1 to N, whether it divides N. If K
         * divides N, it means K is a factor of N
         */
        for (i = 1; i <= N; i++) {
            if (N % i == 0) {
                System.out.print(i + " ");
            }
        }
    }
}
Output
Enter an Integer
50
Factors of 50
1 2 5 10 25 50 

Java program to print all factors of a number using function

This program is similar to above program, except it uses a user defined function "printFactors" which takes a number N as input parameter and print all factors of N.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to print all factors of a number using function
 */
public class PrintFactorsFunction {
    public static void main(String[] args) {
        int N;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter an Integer");
        N = scanner.nextInt();
        // Calling printFactors method to print all
        // factors of N
        printFactors(N);
    }

    /**
     * This method prints all factors of N
     */
    public static void printFactors(int N) {
        int i;
        System.out.format("Factors of %d\n", N);
        /*
         * Check for every number between 1 to N, whether it divides N. If K
         * divides N, it means K is a factor of N
         */
        for (i = 1; i <= N; i++) {
            if (N % i == 0) {
                System.out.print(i + " ");
            }
        }
    }
}
Output
Enter an Integer
50
Factors of 50
1 2 5 10 25 50