Exercise 63

Write a program in Python language that asks the user to enter an integer n and display it if this number is prime or not.

Solution


# Enter value of the integer n
n = int(input("Enter value of the integer n : "))

# we must use an auxiliary variable j that will count the number of divisors of n
# initialize value of j
j = 0
for i in range(1, n+1):
# we test if i is a divisor of n, then we increment j
if(n%i == 0):
j = j + 1

# The integer n is prim number if and only if j is equal to 2
if( j == 2):
print("The integer ", n , " is a prim number")
else:
print("The integer ", n , " is not a prim number")


Younes Derfoufi
my-courses.net

Leave a Reply