Java program to find the divisors of a number.

Home / Uncategorized / Java program to find the divisors of a number.

Java program to find the divisors of a number.

import java.util.Scanner;
public class Divisors 
{
    public static void main(String[] args)
    {
            System.out.print("Enter any number:");
            Scanner sc=new Scanner(System.in);
            int num=sc.nextInt();
            System.out.println("The divisors of "+num+" are:");
            for(int i=1;i<=num;i++)
            {
                if(num%i==0)
                {
                    System.out.print(" "+i);
                }
            }
            System.out.println();
    }
}

Output:

Enter any number:24
The divisors of 24 are:
 1 2 3 4 6 8 12 24

Watch video of this program

Leave a Reply

Your email address will not be published. Required fields are marked *