In dit voorbeeld leert u het grootste getal te vinden tussen de drie cijfers die de gebruiker heeft ingevoerd.
Om dit voorbeeld te begrijpen, moet u kennis hebben van de volgende C-programmeeronderwerpen:
- C Programmeringsoperatoren
- C if… else Statement
Voorbeeld 1: if-instructie gebruiken
#include int main() ( double n1, n2, n3; printf("Enter three different numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); // if n1 is greater than both n2 and n3, n1 is the largest if (n1>= n2 && n1>= n3) printf("%.2f is the largest number.", n1); // if n2 is greater than both n1 and n3, n2 is the largest if (n2>= n1 && n2>= n3) printf("%.2f is the largest number.", n2); // if n3 is greater than both n1 and n2, n3 is the largest if (n3>= n1 && n3>= n2) printf("%.2f is the largest number.", n3); return 0; )
Voorbeeld 2: If… else Ladder gebruiken
#include int main() ( double n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); // if n1 is greater than both n2 and n3, n1 is the largest if (n1>= n2 && n1>= n3) printf("%.2lf is the largest number.", n1); // if n2 is greater than both n1 and n3, n2 is the largest else if (n2>= n1 && n2>= n3) printf("%.2lf is the largest number.", n2); // if both above conditions are false, n3 is the largest else printf("%.2lf is the largest number.", n3); return 0; )
Voorbeeld 3: Genest gebruiken als… anders
#include int main() ( double n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1>= n2) ( if (n1>= n3) printf("%.2lf is the largest number.", n1); else printf("%.2lf is the largest number.", n3); ) else ( if (n2>= n3) printf("%.2lf is the largest number.", n2); else printf("%.2lf is the largest number.", n3); ) return 0; )
De output van al deze bovenstaande programma's zal hetzelfde zijn.
Voer drie cijfers in: -4,5 3,9 5,6 5,60 is het grootste getal.