Factors of a Number in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In this article, we explore various methods in Java to find all factors of a given number. Factors are numbers that divide the original number without leaving a remainder. We'll use loops to iterate from 1 to the number, identifying and listing or printing each factor. This guide provides a comprehensive approach to understanding factor determination in Java programming.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Factors of a Number Using Loop

Our first approach to finding the factors of a number is using the for loop. We run a for loop from 1 to the number n, which we need to find the factors of. Then, we check for those numbers which perfectly divide n, and hence obtain the factors.

Code:

Output:

The same can be done using while or do-while loop. The logic remains the same for both, only syntax changes.

Factors of a Number Using while loop

Factors of a Number Using do-while loop

Output for the above codes remains the same.

The above-discussed approaches are naive to find all the factors of a number. We are simply iterating from 11 to nn, checking if that number divides nn, and printing it. The time complexity of the approach is O(n)O(n), and auxiliary space required is O(1)O(1).

So, can we improve the above approach? The answer is yes. We will now discuss a better approach for finding factors of a number.

Another Approach

If we carefully observe all the factors of a number, then we can say they are present in pairs.

Example: For n = 100, the various pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10). Similarly, for n = 40, the various pairs of divisors are: (1,40), (2,20), (4,10), (5,8).

This remains true for all other numbers, and we can use this fact to devise a better solution. We only need to iterate to the number's square root, which will give us all the pairs. It is important to note that for equal pairs (like (10,10)), the number is considered only once.

Below is the implementation for the same.

Output

Time Complexity: O(sqrt(n))

Auxiliary Space : O(1)

The solution gives all the factors of the number, but it can still be improved since all the factors are not sorted. We can modify the code in our favor so that it prints all factors in sorted order.

For that, we will first print half of all the pairs since they will be sorted. This will give us the numbers which divide the number completely, and then we can print the quotient for each number to get other half.

Code:

Output:

Here also, time complexity remains the same O(sqrt(n)).

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Factors of Negative Number

To find factors of a negative number, we can calculate all of its positive factors, then duplicate them and write a negative sign in front of the duplicates. We can also do this by traversing from n-n to nn to print all factors. We have to exclude 00 since it will give an error.

Code:

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Conclusion

Finally, let's summarise everything we have learned in the article:

  • Numbers that divide the given number completely, leaving 00 remainder, are called factors of the given number.
  • In programming, we use loops such as for, while, and do while to find the factors of a number by simply iterating over all the numbers from 11 to nn to check factors.
  • The time complexity to calculate all factors by traversing 11 to nn is O(n)O(n).
  • The factors of the number do occur in pairs. Thus time complexity to calculate all the factors can be improvised to O(sqrt(n))O(sqrt(n)).