DEV Community

Vinoth Kumar
Vinoth Kumar

Posted on

TODAY SOLVED PROBLEMS PY

Q1)Write a program that takes a number from the user and prints whether it is prime or not prime.

SOLN-1 PYTH:

n = 1319
d = 2
while d <= n//2:
    if n%d == 0:
        print('not a prime')
    d+=1
else:
    print(d)
    print('prime')
Enter fullscreen mode Exit fullscreen mode
  1. In the name of 'n' variable is used to store the value which is '1319', and in another variable name 'd' to store the value '2'.
  2. Here,i am used while loop to check the condition d<=n//2 which is d=2->.
  3. If 2<=1319 then it's get into the loop and check another condition which i given inside the loop is another 'if'.
  4. To check 1319%2 == 0 if this condition satisfied then i used to print('not a prime).

SOLN-2 :

n = 1319
d=2
while d<n:
    if n%d ==0:
        print('not a prime')
        break
    d+=1
else:
    print(n)
    print('prime')
Enter fullscreen mode Exit fullscreen mode

SOLN-3 :

p=7
d=2
if d%p == 0:
    print('not a prime')
    d+=1
else:
    print(d)
    print('prime')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)