{"id":10114,"date":"2021-09-30T12:00:16","date_gmt":"2021-09-30T06:30:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=10114"},"modified":"2021-11-22T18:35:26","modified_gmt":"2021-11-22T13:05:26","slug":"python-program-to-create-a-class-and-compute-the-area-and-the-perimeter-of-the-circle","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-create-a-class-and-compute-the-area-and-the-perimeter-of-the-circle\/","title":{"rendered":"Python Program to Create a Class and Compute the Area and the Perimeter of the Circle"},"content":{"rendered":"
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs<\/a> examples and excel in coding the complex logic.<\/p>\n Object-Oriented Programming(OOPS):<\/strong><\/p>\n Object-Oriented Programming (OOP) is a programming paradigm based on the concepts of classes and objects. It is used to organize a software program into simple, reusable code blueprints (typically referred to as classes), which are then used to build individual instances of objects. Object-oriented programming languages include JavaScript, C++, Java, and Python, among others.<\/p>\n A class is a generic blueprint that can be used to generate more specific, concrete things. Classes are frequently used to represent broad groups, such as Cars or Dogs, that share property. These classes indicate what properties, such as color, an instance of this type will have, but not the value of those attributes for a specific object.<\/p>\n An object comprises data, such as the raw or preprocessed materials at each step of an assembly line, as well as behavior, such as the action performed by each assembly line component.<\/p>\n Python, like other general-purpose programming languages, has always been an object-oriented language. It enables us to create a program with an Object-Oriented approach. Classes and objects are simple to build and utilize in Python.<\/p>\n Given the radius of the circle, the task is to write the python program using classes that calculate the area and perimeter of the circle.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach for writing a Python program that creates a class with two methods that calculate the area and perimeter of the circle.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic. Object-Oriented Programming(OOPS): Object-Oriented Programming (OOP) is a programming paradigm based on the concepts of classes and objects. It is used to organize a software program into …<\/p>\ngiven radius = 13.5<\/pre>\n
The Perimeter of circle with given radius 13.5 = 84.823\r\nThe Area of circle with given radius 13.5 = 572.5553<\/pre>\n
given radius = 20.98<\/pre>\n
The Perimeter of circle with given radius 20.98 = 131.8212\r\nThe Area of circle with given radius 20.98 = 1382.8047<\/pre>\n
Program to Create a Class and Compute the Area and the Perimeter of the Circle<\/h2>\n
\n
1)Using Classes(Static Input)<\/h3>\n
\n
# importing math module\r\nimport math\r\n# creating a class\r\n\r\n\r\nclass circleClass():\r\n # use a parameterized constructor to initialize its value (radius of the circle).\r\n def __init__(self, circleradius):\r\n self.circleradius = circleradius\r\n # Create two methods inside the class.\r\n # printArea: which calculates the area of the circle and returns it.\r\n\r\n def printArea(self):\r\n return math.pi*(self.circleradius**2)\r\n # printPerimeter: which calculates the perimeter of the circle and returns it.\r\n\r\n def printPerimeter(self):\r\n return 2*math.pi*self.circleradius\r\n\r\n\r\n# Give the radius of the circle as static input and store it in a variable.\r\ncircleradius = 13.5\r\n# Create an object to represent the class.\r\ncircleobj = circleClass(circleradius)\r\n# Call both methods with the object created above.\r\n# Hence the area and perimeter of the circle are calculated and printed.\r\nprint(\"The Perimeter of circle with given radius\",\r\n circleradius, '=', round(circleobj.printPerimeter(), 4))\r\nprint(\"The Area of circle with given radius\", circleradius,\r\n '=', round(circleobj.printArea(), 4))\r\n<\/pre>\n
The Perimeter of circle with given radius 13.5 = 84.823\r\nThe Area of circle with given radius 13.5 = 572.5553<\/pre>\n
\n
2)Using Classes(User Input)<\/h3>\n
\n
# importing math module\r\nimport math\r\n# creating a class\r\n\r\n\r\nclass circleClass():\r\n # use a parameterized constructor to initialize its value (radius of the circle).\r\n def __init__(self, circleradius):\r\n self.circleradius = circleradius\r\n # Create two methods inside the class.\r\n # printArea: which calculates the area of the circle and returns it.\r\n\r\n def printArea(self):\r\n return math.pi*(self.circleradius**2)\r\n # printPerimeter: which calculates the perimeter of the circle and returns it.\r\n\r\n def printPerimeter(self):\r\n return 2*math.pi*self.circleradius\r\n\r\n\r\n# Give the radius of the circle as user input using float(input()) and store it in a variable.\r\ncircleradius = float(input('Enter some random radius of the circle = '))\r\n# Create an object to represent the class.\r\ncircleobj = circleClass(circleradius)\r\n# Call both methods with the object created above.\r\n# Hence the area and perimeter of the circle are calculated and printed.\r\nprint(\"The Perimeter of circle with given radius\",\r\n circleradius, '=', round(circleobj.printPerimeter(), 4))\r\nprint(\"The Area of circle with given radius\", circleradius,\r\n '=', round(circleobj.printArea(), 4))\r\n<\/pre>\n
Enter some random radius of the circle = 20.98\r\nThe Perimeter of circle with given radius 20.98 = 131.8212\r\nThe Area of circle with given radius 20.98 = 1382.8047<\/pre>\n
\n