Python Program to check a number is prime or not using constructor


In this program, you will learn how to check a number is prime or not using a constructor in Python.


Some list of prime numbers : 2 3 5 7 11 13 17 19

Example: How to check a number is prime or not using constructor in Python

class Test:

    def __init__(self, num):
        self.num = num

    def check(self):
        i = 2
        f = 1
        while i <= self.num / 2:
            if self.num % i == 0:
                f = 0
                break
            i = i + 1

        if f == 1:
            print("Number is prime:", self.num)
        else:
            print("Number is not prime:", self.num)


num = int(input("Enter a number:"))
obj = Test(num)
obj.check()

Output:

Enter a number:13
Number is prime: 13

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.