Java Program to Display Factors of a Number

Java Program to Display Factors of a Number

What is Factorization?

Factorization is the process of finding the factors or the components that when multiplied together, produce a given number. In mathematics, it is commonly used in finding prime factors, which are the basic building blocks of any positive integer. Factorization is an important concept in many areas of mathematics, including number theory, algebra, and cryptography.

In this Article, we will write a program to Display Factors of a Number.

Factors of a Number :

The factors of a number are the integers that divide the number exactly, without leaving a remainder. For example, the factors of 6 are 1, 2, 3, and 6, because 1 x 6 = 6, 2 x 3 = 6. If a and b are factors of a number, then a x b is also a factor of the number. A number that has only two factors, 1 and itself, is called a prime number.

Example :
The factors of 30 are :   1 , 2 , 3 , 5 , 6 , 10 , 15 , 30
The factors of 42 are : 1 , 2 , 3 , 6 , 7 , 14 , 21 , 42

Program to calculate Factors of a Positive Integer :

// Importing the required packages
import java.util.Scanner;
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Scanner class for taking input  
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a positive integer: ");
    int n = sc.nextInt();
    
    // Printing the factors
    System.out.print("The factors of " + n + " are: ");
    
    // lopp for taking positive factors of an Integer
    for (int i = 1; i <= n; i++) {
      if (n % i == 0) {
        System.out.print(i + " ");
      }
    }
    System.out.println();
  }
}
Output:

Enter a positive integer: 10
The factors of 10 are: 1 2 5 10

Explanation :

The above program uses a for loop to iterate through all the numbers from 1 to n, and checks if each number is a factor of n by using the modulo operator (%). If the result of n % i is 0, then i is a factor of n, and it is printed on the screen.

Program to calculate Factors of a Negative Integer :

Run
// Importing the required packages
import java.util.Scanner;
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Scanner class for taking input  
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a negative integer: ");
    int n = sc.nextInt();
    
    // Printing the factors
    System.out.print("The factors of " + n + " are: ");
    
    // lopp for taking positive factors of an Integer
    for (int i = -1; i >= n; i--) {
      if (n % i == 0) {
        System.out.print(i + " ");
      }
    }
    System.out.println();
  }
}
Output:

Enter a negative integer: -10
The factors of -10 are: -1 -2 -5 -10

Explanation:

This program is similar to the previous one, but it starts the for loop from -1 and decrements i until it reaches n. If the result of n % i is 0, then i is a factor of n, and it is printed on the screen. Note that this program will also print the positive factors of n.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription