Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C ตรวจสอบเลขบวก ลบ หรือศูนย์


เลขที่มากกว่า 0 คือบวก น้อยกว่า 0 คือลบ แนวคิดบวก/ลบสำคัญในทฤษฎีจำนวนและการเขียนโปรแกรม การคำนวณอาศัยแนวคิดนี้

Input: 0 Output: 0 is zero

คำอธิบาย

ใช้ Conditional Statement ตรวจสอบตัวเลขกับ 0 ว่ามากกว่า น้อยกว่า หรือเท่ากัน

ตัวอย่าง

#include <iostream>
using namespace std;
int main() {
    int n = 0;
    if (n > 0) {
        printf("%d is positive", n);
    } else if (n < 0) {
        printf("%d is negative", n);
    } else {
        printf("%d is zero", n);
    }
    return 0;
}