Python Exercise 1 - Solution

Spandan Sehgal - Mar 4 '22 - - Dev Community

Intro

Hello everyone welcome back to the Python Exercises series .
In previous post I asked you all a question and that was
Make a python program to find cube, square and square root of a number given by user
But after three days when I saw that no one has commented their answers I got disappointed . But its ok.

Answer :

#Python Code to print Square, Sqaure Root and Cube
#---------------------------------------------------

'''
The below is the basic method, I have only written the code to find Sqaure Root as other operations will be of same process

number = int(input("Enter the number: \n"))
sqrt = number ** 0.5 #or 1/2
sqaure = number*number
cube = number*number*number
print(f"The sqaure root of {number} is {sqrt}, The Square of {number} is {square} and the Cube of {number} is {cube}")
'''

# -------------------------------------------------------

'''
Below is the another method to do it and it is easy too,
by making a class it is easy to do this task, it also makes the code clean if we need
to add many operations
'''
#--------------------------------------------------------

class operations:
    def __init__(self,square,sqrt,cube): 
       self.square = square*square
       self.sqrt = sqrt**0.5 #or 1/2
       self.cube = cube*cube*cube

num = int(input("Enter a number \n"))
Result = operations(num,num,num)

print(f"The results are: Sqaure - '{Result.square}', Sqaure Root - '{Result.sqrt}', Cube - '{Result.cube}'")


input("") #So that the program doesn't exits

Enter fullscreen mode Exit fullscreen mode

So this was the answer you all can use it .

That's it for today meet you soon, till then stay safe and do take very good care of yourselves .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .