Write a Program to print Factors of a Positive Integer


This Java program finds all the positive factors of a given positive integer. A factor of a positive integer is a positive integer that divides the given integer without leaving a remainder.

The program starts by importing the Scanner class, which allows the program to read input from the user. The program then creates an instance of the Scanner class and prompts the user to enter a positive integer. The program reads the input number using the nextInt() method of the Scanner class and stores it in the integer variable num.

Next, the program enters a for loop that runs from 1 to the input number (num). For each value of i, the program checks if i is a factor of num by using the modulo operator (%). If num is divisible by i with no remainder, then i is a factor of num, and the program prints the value of i using the println() method of the System.out object. Finally, the program ends.

Note that this program only finds positive factors of a positive integer. If you need to find all factors (including negative factors) of a given integer, you would need to modify the program accordingly.

Source Code

import java.util.Scanner;
class Factors_Positive
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Positive Number :");
		int num = input.nextInt();
		System.out.println("Factors Positive Integer are: ");
		for (int i = 1; i <= num; ++i)
		{
			if (num % i == 0)
			{
				System.out.println(i);
			}
		}
	}
}

Output

Enter The Positive Number :34
Factors Positive Integer are:
1
2
17
34

Example Programs