Exercise 15
Write a python program that asks the user to enter an integer n and to display whether this number is prime or not.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Ask user to enter an integer n n = int(input("Type the value of n: ")) # we use a counter that counts the number of divisors of n d = 0 for i in range(1, n+1): if(n%i == 0): d = d + 1 # We test if the number of divisors of n is = 2 to conclude that n is prime if( d == 2): print("The number ", n , " is prime") else: print("The number ", n , " is not prime") |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Solution Exercise 15 an algorithm python totest if a number is prime or not”