Perimeter of rectangle: Perimeter of rectangle is given by the below formula is, Perimeter = 2 * (height + width)
Approach-1:
- In this approach, we define the complete logic in main() function.
- We read the required values like height and width of rectangle and find the perimeter value using simple formula.
#include <stdio.h>
int main()
{
float h, w, p;
printf("Finding perimeter of rectangle\n");
printf("Enter height : ");
scanf("%f", &h);
printf("Enter width : ");
scanf("%f", &w);
p = 2*(h + w);
printf("Perimeter of rectangle is = %f units ", p);
return 0;
}
Approach-2:
- In this approach we use custom defined function to find the perimeter value of rectangle.
- We read the required input in the main() function and invokes user function perimeter().
- Custom defined function returns the result to calling function after processing the input values.
#include <stdio.h>
float perimeter(float, float);
int main()
{
float h, w, p;
printf("Finding perimeter of rectangle\n");
printf("Enter height : ");
scanf("%f", &h);
printf("Enter width : ");
scanf("%f", &w);
p = perimeter(h,w);
printf("Perimeter of rectangle is = %f units ", p);
return 0;
}
float perimeter(float h, float w)
{
float p = 2*(h+w);
return p;
}