Home » Java programs » Java basic programs

Java program to count factors of a given number

Here, we are implementing a java program that will read an integer number and count its all factors. If a number is divisible by 4 different numbers than factors of that number will be 4.
Submitted by IncludeHelp, on December 10, 2017

Given an integer number and we have to count its all factors using java program.


    Enter the number : 45
    Number of factors of is : 6
    45 is divisible by: 1, 3, 5, 9, 15, 45
    Thus, factors will be 6

Program to count all factors of a number in java

import java.util.Scanner;

public class CountFactors
	public static void main(String[] args)
		// create object
		Scanner in = new Scanner(System.in);
		// enter integer number here.
	    System.out.print("Enter the number : ");
	    int x = in.nextInt(); 
	    System.out.println("Number of factors of is : " +result(x));
	// create function to find the factors of given number.
	public static int result(int num) 
		int ctr = 0;
		for(int i=1; i<=(int)Math.sqrt(num); i++)
	        if(num%i==0 && i*i!=num)
	        else if (i*i==num) 
	        return ctr;


First run:
Enter the number : 45
Number of factors of is : 6

Second run:
Enter the number : 158
Number of factors of is : 4

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.