PROBLEM #1: Finding and Visualizing the area and perimeter of a Rectangle

4 0 0
                                    

***

#include <stdio.h>

int area(int , int);
int perimeter(int, int);

int main(){
int length, width;

printf("Enter the length and width of the rectangle: ");
scanf("%d%d", &length, &width);


for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
(i == 0 || j == 0 || i == length - 1 || j == width - 1) ?
printf("* "): printf(" ");
}
printf("

");
}


if(length < width){
length += width;
width = length - width;
length -= width;
}


printf("FORMULA

");
printf("\tPERIMETER = 2(length + width)

");
printf("\t%-9d = 2(%-6d + %d)

", perimeter(length, width), length, width);
printf("\tThe perimeter of the rectange is %d

", 2 * (length + width));

printf("FORMULA

");
printf("\tAREA = length * width

");
printf("\t%-4d = %-6d * %d

", area(length, width), length, width);
printf("\tThe area of the rectangle is %d

", length * width);

return 0;
}

int area(int length, int width){
return length * width;
}

int perimeter (int length, int width){
return 2 * (length + width);
}

***

Programming in C/C++ Where stories live. Discover now