A program that calculates the square, cube and square root of a given number

 💓💓💓💓

Hello, dear ones!

The following code can be used to calculate the square, cube and square root of a given number.


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

    def square(self):
        print(f"The square of {self.num} is {self.num**2}")
   
    def cube(self):
        print(f"The cube of {self.num} is {self.num**3}")
   
    def sqrt(self):
        print(f"The sqrt of {self.num} is {self.num**0.5}")


calc = Calculator(6)
calc.square()
calc.cube()
calc.sqrt()


Note: The above program will return the square, cube, and square root of a given number because all the following three functions are being called, i.e.

calc.square()

calc.cube()

calc.sqrt()

*If you want to find the square of a given number only, put the given number in the Calculator class and call the respective function. In this case it is;

calc.square()

*If you want to find both the square and cube of a given number, put the given number in the Calculator class and call the respective functions. In this case, it is;

calc.square()

calc.cube()


I am sure you got the point. 👍👍👍👍💓

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes