-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQuestion9.py
More file actions
37 lines (28 loc) · 986 Bytes
/
Copy pathQuestion9.py
File metadata and controls
37 lines (28 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Question :-
# WAP to find if a given number is prime or not :-
# CODE :-
# I ask the user to give a number to check if its prime or not
num=int(input('Enter a number: '))
# Primes are greater than 1
if num>1:
# Now I introduced a range from which to check if the number is a prime or not
for i in range(2,num):
if num%i==0:
print(f'{num} is not a prime number.')
print(f'{i} times {num//i} is {num}.')
break
else:
print(f'{num} is a prime number.')
# if the entered number is <= 1 or =1, then its not a prime number
else:
print(f'{num} is not a prime number.')
# Addtional Comments
# I have used formatted strings but you can use whatever method you are comfortable with. Check next line...
# For example - print(num,'is not a prime number') or print(i,"times",num,"is",num)
# OUTPUT:-
# Enter a number: 2
# 2 is a prime number.
# OR
# Enter a number: 4
# 4 is not a prime number.
# 2 times 4 is 4.