This program calculates the area, perimeter, and diagonal of a rectangle based on its width and height.
#include <stdio.h>
#include <math.h>
int main()
{
float width, height, area, parimeter, diagonal;
printf("Rectangle Calculator: \n\n");
printf("--------width--------- \n");
printf("| | \n");
printf("| | height\n");
printf("| | \n");
printf("| | \n");
printf("---------------------- \n");
printf("Width: ");
scanf("%f", &width);
printf("Height: ");
scanf("%f", &height);
printf("--------------------- \n");
area = width * height;
parimeter = (2 * width) + (2 * height);
diagonal = sqrt((width*width) + (height*height));
printf("Area: %.2f \n", area);
printf("Parimeter: %.2f \n", parimeter);
printf("Diagonal: %.2f \n", diagonal);
printf("--------------------- \n");
return 0;
}
Result:
Rectangle Calculator:
--------width---------
| |
| | height
| |
| |
----------------------
Width: 100
Height: 50
---------------------
Area: 5000.00
Parimeter: 300.00
Diagonal: 111.80
---------------------
Process returned 0 (0x0) execution time : 4.192 s
Press any key to continue.
Here's a line-by-line explanation of the code:
#include <stdio.h>
#include <math.h>
int main()
{
This code defines the standard library header files that will be used in the program, including stdio.h
and math.h
. It also defines the main function, which is the entry point of the program.
float width, height, area, parimeter, diagonal;
This code declares five float variables, namely width
, height
, area
, perimeter
, and diagonal
. These variables will be used to store the input values for the width and height of the rectangle and the calculated values of its area, perimeter, and diagonal.
printf("Rectangle Calculator: \n\n");
printf("--------width--------- \n");
printf("| | \n");
printf("| | height\n");
printf("| | \n");
printf("| | \n");
printf("---------------------- \n");
This code displays a prompt to the user asking for the width and height of the rectangle, and also shows a visual representation of the rectangle.
printf("Width: ");
scanf("%f", &width);
printf("Height: ");
scanf("%f", &height);
printf("--------------------- \n");
This code prompts the user to input the width and height of the rectangle, and then stores these values in the width
and height
variables.
area = width * height;
perimeter = (2 * width) + (2 * height);
diagonal = sqrt((width*width) + (height*height));
This code calculates the area, perimeter, and diagonal of the rectangle based on its width and height, and stores the results in the area
, perimeter
, and diagonal
variables, respectively.
printf("Area: %.2f \n", area);
printf("Perimeter: %.2f \n", perimeter);
printf("Diagonal: %.2f \n", diagonal);
printf("--------------------- \n");
return 0;
}
This code displays the calculated values of the area, perimeter, and diagonal of the rectangle using the printf
function. The %.2f
format specifier is used to display the floating-point numbers with two decimal places. Finally, the function returns 0 to indicate successful execution of the program.
No comments:
Post a Comment