C program to find area of a rectangle

Previous Program Next Program

Write a C program to input length and width of a rectangle and find area of the given rectangle. How to calculate area of a rectangle whose length and breadth are given by user in C programming. Logic to find area of a rectangle whose length and width are given in C programming.

Example

Input

Input length: 5
Input width: 10

Output

Area of rectangle = 50 sq. units

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Area of rectangle

Area of rectangle is given by the formula -

Area of rectangle
Where l is length and w is width of the rectangle.

Logic to find area of rectangle

Logic to calculate area of a rectangle is almost similar to the previous program (perimeter of rectangle). We only need to apply a different formula. Below is the step by step descriptive logic to find area of rectangle -

  1. Read length and width of rectangle and store it in two different variables say length and width.
  2. Apply the formula to compute area area = length * width.
  3. Print the resultant area.

Program to find area of rectangle

/**
 * C program to find area of rectangle
 */

#include <stdio.h>

int main()
{
    float length, width, area;

    /*
     * Input length and width of rectangle
     */
    printf("Enter length of rectangle: ");
    scanf("%f", &length);
    printf("Enter width of rectangle: ");
    scanf("%d", &width);

    /* Calculate area of rectangle */
    area = length * width;

    /* Print area of rectangle */
    printf("Area of rectangle = %f sq. units ", area);

    return 0;
} 
Output
Enter length of rectangle: 5
Enter width of rectangle: 10
Area of rectangle = 50.000000 sq. units

Happy coding ;)

Recommended post

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

1 comment: