Calculating Rectangle Area in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to calculate the area of a rectangle using a user-defined function in the C programming language.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/variables -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/data_types -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/operators -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/user_input -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/memory_address -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/pointers -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/function_parameters -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} c/function_declaration -.-> lab-136085{{"`Calculating Rectangle Area in C`"}} end

Find Out the Area of a Rectangle Using a Function

In this lab, you will learn how to find out the area of a rectangle using a function.

  1. Create a new file named rectangle.c in WebIDE.

  2. Add the following code to the rectangle.c file:

    #include <stdio.h>
    
    int area(int h, int w)
    {
      int area = h * w;
      return area;
    }
    
    void main()
    {
      int height, width;
    
      printf("Enter the height of the rectangle: ");
      scanf("%d", &height);
    
      printf("Enter the width of the rectangle: ");
      scanf("%d", &width);
    
      printf("The area of the rectangle = %d\n\n\n", area(height, width));
    }

    This code defines a function area that takes two parameters h and w representing the height and width of the rectangle, respectively. The function calculates the area of the rectangle by multiplying the height and width and returns the result.

    The main function reads the height and width values from the user, calls the area function to calculate the area, and then prints the result.

  3. Save the rectangle.c file.

  4. Open a terminal and navigate to the directory where the rectangle.c file is located.

  5. Compile the rectangle.c file using the following command:

    gcc rectangle.c -o rectangle

    This command compiles the C code and generates an executable file named rectangle.

  6. Run the rectangle executable using the following command:

    ./rectangle
  7. When prompted, enter the height and width of the rectangle.

  8. The program will calculate and print the area of the rectangle.

Summary

After completing this lab, you will be able to calculate the area of a rectangle using a user-defined function in the C programming language.

Other C Tutorials you may like