This is a Python Program to find the area of a rectangle using classes.
The program takes the length and breadth from the user and finds the area of the rectangle using classes.
1. Take the value of length and breadth from the user. 2. Create a class and using a constructor initialise values of that class. 3. Create a method called as area and return the area of the class. 4. Create an object for the class. 5. Using the object, call the method area() with the parameters as the length and breadth taken from the user. 6. Print the area.
7. Exit
Here is the source code of the Python Program to take the length and breadth from the user and find the area of the rectangle. The program output is also shown below.
class rectangle(): def __init__(self,breadth,length): self.breadth=breadth self.length=length def area(self): return self.breadth*self.length a=int(input("Enter length of rectangle: ")) b=int(input("Enter breadth of rectangle: ")) obj=rectangle(a,b) print("Area of rectangle:",obj.area()) print()
1. User must enter the value of length and breadth. 2. A class called rectangle is created and the __init__() method is used to initialise values of that class. 3. A method called as area, returns self.length*self.breadth which is the area of the class. 4. An object for the class is created. 5. Using the object, the method area() is called with the parameters as the length and breadth taken from the user.
6. The area is printed.
Case 1: Enter length of rectangle: 4 Enter breadth of rectangle: 5 Area of rectangle: 20 Case 2: Enter length of rectangle: 15 Enter breadth of rectangle: 13 Area of rectangle: 195
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Check this: Information Technology Books | Programming MCQs
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
View Discussion
Improve Article
Save Article
Like Article
View Discussion
Improve Article
Save Article
Like Article
A rectangle is a flat figure in a plane. It has four sides and four equal angles of 90 degree each. In rectangle all four sides are not of equal length like square, sides opposite to each other have equal length. Both diagonals of the rectangle have equal length.
Examples:
Input : 4 5 Output : Area = 20 Perimeter = 18 Input : 2 3 Output : Area = 6 Perimeter = 10Formulae :
Area of rectangle : a*b Perimeter of rectangle: 2*(a + b)#include<bits/stdc++.h> using namespace std; int areaRectangle(int a, int b) { int area = a * b; return area; } int perimeterRectangle(int a, int b) { int perimeter = 2*(a + b); return perimeter; } int main() { int a = 5; int b = 6; cout << "Area = " << areaRectangle(a, b) << endl; cout << "Perimeter = " << perimeterRectangle(a, b); return 0; } |
import java.io.*; class Geometry { static int areaRectangle(int a, int b) { int area = a * b; return area; } static int perimeterRectangle(int a, int b) { int perimeter = 2*(a + b); return perimeter; } public static void main (String[] args) { int a = 5; int b = 6; System.out.println("Area = "+ areaRectangle(a, b)); System.out.println("Perimeter = "+ perimeterRectangle(a, b)); } } |
def areaRectangle(a, b): return (a * b) def perimeterRectangle(a, b): return (2 * (a + b)) a = 5; b = 6; print ("Area = ", areaRectangle(a, b)) print ("Perimeter = ", perimeterRectangle(a, b)) |
using System; class GFG { static int areaRectangle(int a, int b) { int area = a * b; return area; } static int perimeterRectangle(int a, int b) { int perimeter = 2 * (a + b); return perimeter; } public static void Main() { int a = 5; int b = 6; Console.WriteLine("Area = " + areaRectangle(a, b)); Console.WriteLine("Perimeter = " + perimeterRectangle(a, b)); } } |
<?php function areaRectangle( $a, $b) { $area = $a * $b; return $area; } function perimeterRectangle( $a, $b) { $perimeter = 2 * ($a + $b); return $perimeter; } $a = 5; $b = 6; echo("Area = " ); echo(areaRectangle($a, $b)); echo("\n"); echo( "Perimeter = "); echo(perimeterRectangle($a, $b)); ?> |
<script> function areaRectangle(a, b) { let area = a * b; return area; } function perimeterRectangle(a, b) { let perimeter = 2*(a + b); return perimeter; } let a = 5; let b = 6; document.write("Area = " + areaRectangle(a, b) + "<br>"); document.write("Perimeter = " + perimeterRectangle(a, b)); </script> |
Output :
Area = 30 Perimeter = 22Time complexity : O(1)
Auxiliary Space : O(1)
This article is contributed by Saloni Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.