In this we are going to see a basic program to calculate prime number using classes in Python Programming Language.
import math
class prime:
def __init__(self, x):
self.a = 0
self.k = 0
self.i = 0
self.a = x
def calculate(self):
self.k = 1
self.i = 2
while self.i <= math.sqrt(self.a):
if self.a % self.i == 0:
self.k = 0
break
else:
self.k = 1
self.i += 1
def show(self):
if self.k == 1:
print(self.a, end='')
print(" is a Prime Number.", end='')
else:
print(self.a, end='')
print(" is Not a Prime Number.", end='')
print("Enter a number: ", end='')
a = int(input())
obj = prime(a)
obj.calculate()
obj.show()
#Coded by SAAHIL
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP