Find all divisors of a natural number in java


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

Updated on: 25-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements