PROGRAM prog3A
!Initialise and name the program

	IMPLICIT NONE
	!Assume nothing about the variables within this program
	INTEGER :: n, i, q, r
	!Declare integer variables for the number to be prime checked, the counting var and two process vars
	
	PRINT*, 'Enter your integer for prime checking: '
	!Prompt the user to input the integer to be prime checked
	READ*, n
	!Read the value into variable n
	
	i = 2
	!Set the start point of the counting variable i
	
	IF (n < 2) THEN
	!Check for the case where n < 2 as these n are non-prime
		PRINT*, 'The integer,', n, 'is non-prime'
		!If the case is true, print that n is non-prime
		ELSE IF (n == 2) THEN
		!Check for the case where n = 2 as 2 is prime
		PRINT*, 'The integer, 2 , is prime.'
		!If the case is true, print that 2 is prime
		ELSE
		!If the case is false run the following process
	
		DO
		!Open a DO loop to iterate the factor checking
	  		q = n/i
			!Calculate an INTEGER division of the test integer divided by the counting variable
	  		r = q*i
			!If n is not exactly divisible by i then there will be a rounding error in q +/-0.5
			!so if we multiply q by i and check the result against n we can see if i was a factor
	  		IF (r==n) THEN
			!Check whether r = n
	  			PRINT*, 'The integer,', n, ', is non-prime as it is divisible by ', i
				!If the case is true then print that n is divisible by i and therefore non-prime
				EXIT
				!Exit the DO loop as further computation is unnecessary
	  		ELSE IF (i >= SQRT(1.0*n)) THEN
			!Check if SQRT(n) has been reached as this is an upper bound of the prime factors
			!Note that this condition will always be met so the infinite loop will always end
				PRINT*, 'The integer,', n, ', is prime.'
				!If the case is true then print that n is prime
				EXIT
				!And exit the DO loop as further computation is unnecessary
			END IF
			!Close the IF statement within the DO loop
			i = i + 1
			!Increment the counting variable at the end of the loop
		END DO
		!End the DO loop
	END IF
	!End the initial IF statement

END PROGRAM prog3A
!End the program
