Java Program to Find All Factors of a Number

Here is a 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;

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);

        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;

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);
    }

    public static void printFactors(int N) {
        int i;
        System.out.format("Factors of %d\n", 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

Recommended Posts
Java Program to Find LCM and GCD of Two Numbers
Java Program to Reverse a Number using Recursion
Java Program to Check If a Year is Leap Year or Not
Java Program to Find Factorial of a Number Using Recursion
Java Program to Make a Simple Calculator using Switch Statement
Java Program for Matrix Multiplication
Java Program to generate a sequence of random numbers
Java Program to Print Multiplication Table of Number
Java Program to Check a Number is Prime Number or Not
Java Program to Check Whether a Number is Armstrong Number or Not
Java Program to Convert Decimal to Binary Numbers
All Java Programs