Header Ads Widget

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

 

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

Given length and width of the rectangle , write a C 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 C 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 integer 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() function, declare integer variables l and w to store the length and width of the rectangle respectively.
  5. Ask the user to input the length and width of the rectangle using printf() and scanf() statements.
  6. Call the area() function with arguments l and w and store the returned value in an integer variable a.
  7. Print the area of the rectangle using printf() statement.
  8. End the program.

Code(C):-

#include <stdio.h>

// Function to calculate area of rectangle
int area(int length, int width) {
return length * width;
}

int main() {
int l, w;

printf("Enter length and width of rectangle: ");
scanf("%d %d", &l, &w);

int a = area(l, w);

printf("The area of the rectangle is: %d\n", a);

return 0;
}

Output:

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

Explanation

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


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