Tính chu vi và diện tích hình vuông

Viết chương trình cho phép người dùng nhập vào cạnh a của hình vuông rồi tính chu vi và diện tích của hình vuông. Chương trình phải kiểm tra xem cạnh vuông có phải là một số dương hay không.
 avatar
nguyenthanhtruong
c_cpp
a year ago
1.0 kB
8
Indexable
//Write a program that allows the user to enter the edge called a of the square and then calculates
//the perimeter and area of the square. The program must check whether the square edge is a
//positive number or not.
//Formula
//Perimeter = 4 * a
//Area = a * a
//Example 1: Please enter the edge of the square: -6
//The edge must be a positive number!
//Example 2: Please enter the edge of the square: 0
//The edge must be a positive number!
//Example 3: Please enter the edge of the square: 10
//The perimeter of the square is 40
//The area of the square is 100

#include <stdio.h>
#include <math.h>
int main() {
    int a, perimeter, area;
    printf("Please enter the edge of the square: ", a);
    scanf("%d", &a);
    perimeter = 4 * a;
    area = a * a;
    if (a < 0 || a == 0) {
        printf("The edge must be a positive number!\n", a);
    }
    else{
        printf("The perimeter of the square is %d\n", perimeter);
        printf("The area of the square is %d\n", area);
    }
    return 0;
}
Editor is loading...
Leave a Comment