Tính chu vi và diện tích hình chữ nhật

Viết chương trình cho phép người dùng nhập chiều rộng (w) và chiều cao (h) của hình chữ nhật rồi tính chu vi và diện tích hình chữ nhật đó. Chương trình phải kiểm tra xem chiều rộng và chiều cao có phải là số dương hay không.
 avatar
nguyenthanhtruong
c_cpp
a year ago
1.4 kB
6
Indexable
//Write a program that allows the user to enter the width (w) and the height (h) of the rectangle
//and then calculates the perimeter and area of the rectangle. The program must check whether
//the width and the height is a positive number or not.
//Formula
//Perimeter = 2 * (w + h)
//Area = w * h
//Example 1: Please enter the width of the rectangle: -9
//Please enter the height of the rectangle: 3
//The edge of rectangle must be a positive number!
//Example 2: Please enter the width of the rectangle: 15
//Please enter the height of the rectangle: 0
//The edge of rectangle must be a positive number!
//Example 3: Please enter the width of the rectangle: 5
//Please enter the height of the rectangle: 12
//The perimeter of the rectangle is 34
//The area of the rectangle is 60

#include <stdio.h>
#include <math.h>
int main() {
   int w, h, perimeter, area;
   printf("Please enter the width of the rectangle: ");
   scanf("%d", &w);
   printf("Please enter the height of the rectangle: ");
   scanf("%d", &h);
   perimeter = 2 * (w + h);
   area = w * h;
   if(w < 0 && h > 0 || w > 0 && h < 0 || w == 0 || h == 0) {
        printf("The edge of rectangle must be a positive number!\n",w, h);
   }
   else{
    printf("The perimeter of the rectangle is %d\n", perimeter);
    printf("The area of the rectangle is %d", area);
   }

    return 0;
}
Editor is loading...
Leave a Comment