BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to calculate area of rectangle

Last Updated: May 19, 2024 by Chaitanya Singh | Filed Under: C Programs

In this example, we will write a C program to calculate area of rectangle based on user input. For example, if user enters width as 4 and height as 2 then program should print 8 as area of rectangle.

Area of rectangle = width * height

C Program to calculate area of rectangle based on user input

#include <stdio.h>

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

// Prompts the user to enter width of the rectangle
printf("Enter width of the rectangle: ");
scanf("%f", &width);

// Prompts the user to enter height of the rectangle
printf("Enter height of the rectangle: ");
scanf("%f", &height);

// Calculate the area of the rectangle
area = width * height;

// Print the area of rectangle
printf("Area of the rectangle is: %.2f\n", area);

return 0;
}

Output:

Enter width of the rectangle: 5.5
Enter height of the rectangle: 3.2
Area of the rectangle is: 17.60

Explanation:

  1. Include header file: The stdio.h header file is included to use the printf and scanf functions for input and output.
  2. Declare variables: In this program, we have three variables: width, height and area. These variables store the width, height, and area of the rectangle, respectively.
  3. Input: It prompts the user to enter the width and height of the rectangle using printf. User entered values are read and stored in respective variables using scanf.
  4. Calculate the area: Area of the rectangle is calculated using the formula width * height.
  5. Print the result: Finally, the area is printed using printf.

Top Related Articles:

  1. C Program to read and print employee details using structure
  2. C Program to Read the First Line From a File
  3. C Program to concatenate two strings without using strcat
  4. C Program to find greatest of three numbers
  5. C Program to print current date and time

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap