Prime number Learn programming Java

Lesson:

Flow Control


Exercise:

Prime number 16


Objetive:

Write a java program which asks the user for an integer number and answers if it is a prime number or not.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int number;
		int dividers = 0;

		System.out.print("Enter a number: ");
		number = Integer.parseInt(new Scanner(System.in).nextLine());

		for (int i = 1; i <= number; i++)
		{
			if (number % i == 0)
			{
				dividers++;
			}
		}

		if (dividers == 2)
		{
			System.out.println("The number is prime");
		}
		else
		{
			System.out.println("The number is not prime");
		}
	}
}