Find all divisors of a natural number in java

Java Programming Java8Java.Util Package

Following is the Java program which prints all the divisors of a given number.

Program

import java.util.Scanner;

public class DivisorsOfNaturalNumber {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter required number :");
      int num = sc.nextInt();
     
      for(int i = 1; i<num; i++) {
         if(num % i == 0) {
            System.out.println(" "+i);
         }
      }
   }
}

Output

Enter required number :
200
1
2
4
5
8
10
20
25
40
50
100
raja
Published on 04-Jun-2018 13:09:57
Advertisements