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

โปรแกรม C หาตัวเลขที่มากที่สุดใน 3 ตัว


โปรแกรมนี้รับ 3 ตัวเลข และหาตัวที่มากที่สุด โดยเปรียบเทียบกันทีละคู่

Input: a=2, b=4, c=7 Output: 7 Largest Number

คำอธิบาย

ใช้คำสั่ง if 巢狀 (Nested if) เพื่อหาค่ามากที่สุด

ตัวอย่าง

#include <iostream>
using namespace std;
int main() {
    int a, b, c;
    a = 2; b = 4; c = 7;
    if (a > b) {
        if (a > c) {
            printf("%d Largest Number ", a);
        } else {
            printf("%d Largest Number ", c);
        }
    } else {
        if (b > c) {
            printf("%d Largest Number ", b);
        } else {
            printf("%d Largest Number ", c);
        }
    }
    return 0;
}