JAVA Program to print factors of a number

the school programmer


factor is a number that divides into another number exactly and without leaving a remainder. The number 60 has twelve factors:

1 2 3 4 5 6 10 12 15 20 30 60

If 60 is divided by any of the twelve factors then the answer will be a whole number.

For example:
60 ÷ 12 = 5
60 ÷ 15 = 4


Join Us

For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..



Steps to be followed for printing factors of a number.

  1. Input a number from user , let it be N.
  2. Create a loop which starts from i=1 and ends at i<=N .
  3. Now if N%i==0 , print that number .

Sounds Good ? Let's get started

CODE:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Author: TheSchoolProgrammer
 * www.theschoolprogrammer.com
 * Question:Write a program to accept a number and print factors of that number.
 */
import java.util.*;
public class Factor
{
public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER A NUMBER");
        int n=sc.nextInt();
        int i;
        for(i=1;i<=n;i++)
        {
            if(n%i==0)
            System.out.print(i+" ");
        }
    }
}

Output:

Example 1
Input: 12
Output:1 2 3 4 6 12

Example 2
Input:15
Output:1 3 5 15




Found this useful? Share with your friends. Share

Comments

You are welcome to share your ideas with us in comments!!!

Full Screen Mode

Archive

Contact Form

Send