Header Ads Widget

Write a Python program with algorithm and explanation to print area of rectangle using function & return its value to main function.

 

 Write a Python program to print area of rectangle using function & return its value to main function  

Given length and width of the rectangle , write a Python program to print area of rectangle using function & return its value to main function  .

Sample input

4 
3
Sample output

The area of the rectangle is: 12

Write a Python program to print area of rectangle using function & return its value to main function  

The objective of the code is to find the area of the rectangle and return it to the main function that is driver function of the code .

Algorithm:

  1. Start the program.
  2. Define the area() function with two arguments length and width.
  3. Inside the area() function, multiply the length and width of the rectangle and return the product as the area of the rectangle.
  4. In the main program, ask the user to input the length and width of the rectangle using input() function and store them in variables l and w.
  5. Call the area() function with arguments l and w and store the returned value in a variable a.
  6. Print the area of the rectangle using print() function.
  7. End the program.

Code(Python):-

def area(length, width):
return length * width

if __name__ == '__main__':
l = int(input("Enter length of rectangle: "))
w = int(input("Enter width of rectangle: "))
a = area(l, w)
print(f"The area of the rectangle is: {a}")


Output:

Enter length of rectangle: 4
Enter width of rectangle: 3
The area of the rectangle is: 12

Explanation

In this program, the area() function takes two arguments length and width and returns their product as the area of the rectangle. The main program asks the user to input the length and width of the rectangle using input() function and stores them in variables l and w. It then calls the area() function with arguments l and w and stores the returned value in a variable a. Finally, it prints the area of the rectangle using print() function.


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments