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:
- The program starts with the inclusion of the standard input-output library,
stdio.h
, which provides functions likeprintf
andscanf
. - The
main()
function is the entry point of the program, where the execution begins. - Inside the
main()
function, three integer variables are declared:length
,width
, andarea
. These variables will be used to store the user input and calculated area. - The
printf
function is used to display the prompt message: “Enter a length and width:” to the user. - 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 thelength
andwidth
variables, respectively. - The area of the rectangle is calculated by multiplying the length and width together, and the result is stored in the
area
variable. - 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. - 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.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.