Python Program For Prime Numbers

0



Python Program For Prime Numbers


1.Python Program to Check Prime Number


In this program, the user is asked to enter a number, which is then stored in the variable num. If the entered number is greater than 1, then the program checks whether it has any factors other than 1 and itself. It does this by iterating through all the numbers from 2 to num-1 using a for loop. If the entered number has any factor other than 1 and itself, then it is not a prime number, and the program breaks out of the loop and prints a message indicating that the number is not prime. If the entered number does not have any factor other than 1 and itself, then it is a prime number, and the program prints a message indicating that the number is prime.


If the entered number is less than or equal to 1, then it is not considered prime and the program prints a message indicating that the number is not prime. 


Input Program :

num = int(input("Enter a number: "))


# prime numbers are greater than 1

if num > 1:

    # check for factors

    for i in range(2, num):

        if (num % i) == 0:

            print(num, "is not a prime number")

            break

    else:

        print(num, "is a prime number")


# if input number is less than or equal to 1, it is not prime

else:

    print(num, "is not a prime number")


Output Of Program :

Enter a number: 2
2 is a prime number
&
Enter a number: 66
66 is not a prime number


2.Python Program to Print All Prime Numbers in an Interval

In this program, the user is asked to enter the lower and upper limits of the interval. The program then prints a message indicating the interval it will be checking for prime numbers.

Then, the program uses a for loop to iterate through all the numbers in the given interval. For each number, it checks whether it is greater than 1 and whether it has any factors other than 1 and itself. If the number is greater than 1 and does not have any factors other than 1 and itself, then it is a prime number and the program prints it.

Note that the program uses a nested for loop to check whether a given number is prime. The outer for loop iterates through all the numbers in the given interval, and the inner for loop checks whether a given number has any factors other than 1 and itself. If the inner loop finds any such factor, it immediately breaks out of the loop and the number is deemed not prime. If the inner loop completes without finding any such factor, the number is deemed prime and the program prints it.


Input Program :

lower = int(input("Enter the lower limit: "))
upper = int(input("Enter the upper limit: "))

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
   # prime numbers are greater than 1
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)


Output of Program :

Enter the lower limit: 1
Enter the upper limit: 100
Prime numbers between 1 and 100 are:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97



Post a Comment

0Comments
Post a Comment (0)