C program to find area and Perimeter of Rectangle

#include<stdio.h>
int main()
{

    int length,wight,area=0;

    printf("Enter a length and wight:");
    scanf("%d %d",&length,&wight);

    area=length*wight;

   printf("area of rectangle is:%d",area);

return 0;

}

C program to find area and Perimeter of Rectangle

This code is a simple C program that calculates the area of a rectangle based on user input for the length and width of the rectangle. Let’s break down the code and understand it step by step:

  1. The program starts with the inclusion of the standard input-output library, stdio.h, which provides functions like printf and scanf.
  2. The main() function is the entry point of the program, where the execution begins.
  3. Inside the main() function, three integer variables are declared: length, width, and area. These variables will be used to store the user input and calculated area.
  4. The printf function is used to display the prompt message: “Enter a length and width:” to the user.
  5. The scanf function is used to read the user input for length and width. The format specifier %d %d is used to read two integers separated by a space, and the values are stored in the length and width variables, respectively.
  6. The area of the rectangle is calculated by multiplying the length and width together, and the result is stored in the area variable.
  7. Finally, the printf function is used to display the calculated area to the user with the message “Area of rectangle is: %d”, where %d is a placeholder for the integer value of the area variable.
  8. The return 0; statement indicates the successful execution of the program, and the value 0 is returned to the operating system.

To summarize, this program prompts the user to enter the length and width of a rectangle, calculates the area by multiplying these values, and then displays the calculated area to the user.

Write a program in C to display the cube of the number upto given an integer.

1 thought on “C program to find area and Perimeter of Rectangle-1”

Leave a Comment