In: Computer Science
Write a program 'Rectangle-Area.c' that inputs the length and width of a rectangle and outputs its area. The program consists of two functions: the main function and a function that computes and returns the area of a rectangle. The output of the program should be in the main program, not in the function.
Sample Input: 5 8 Sample Output: The area of a 5 by 8 rectangle is 40.
Program code to copy:-
/*Program inputs the length and width of a rectangle and
outputs its area.
This program consists of two functions:
- main() function which takes length and width as input from
user and calls computeArea() function by passing length and
width as parameter
- a computeArea() function computes and returns the
area of a rectangle.
*/
#include <stdio.h>
/*Function prototype*/
int computeArea(int l, int w);
int main()
{
int length, width, area;
/*Prompt the user to input length and width of
rectangle*/
printf("Enter the length of rectangle: ");
scanf("%d", &length);
printf("\nEnter the width of rectangle: ");
scanf("%d", &width);
/*Calling function to computer area by passing length
and
width as parameter*/
area = computeArea(length, width);
printf("\nThe area of a %d by %d rectangle is %d\n",
length, width, area);
}
/*Function computes and returns the area of a rectangle
and
receives two int type parameter */
int computeArea(int l, int w)
{
/*Calculate area of rectangle*/
int area = l * w;
/*Returns area to main() function*/
return area;
}
Screenshot of output:-