Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Chương trình C tính diện tích hình tròn

Diện tích hình tròn = πr² hoặc πd²/4 (d = đường kính = 2r).

Code dùng bán kính

#include <stdio.h>
int main(void) {
    float pi = 3.14;
    int r = 6;
    printf("Ban kinh: %d\n", r);
    float area = pi * r * r;
    printf("Dien tich: %f", area);
    return 0;
}

Kết quả: 113.040001

Code dùng pow() từ math.h

#include <stdio.h>
#include <math.h>
int main(void) {
    float pi = 3.14;
    int r = 6;
    float area = pi * pow(r, 2);
    printf("Dien tich: %f", area);
    return 0;
}

Code dùng đường kính

#include <stdio.h>
int main(void) {
    float pi = 3.14;
    int d = 12;
    float area = (pi * d * d) / 4;
    printf("Dien tich: %f", area);
    return 0;
}

Kết quả: 113.040001