C Language Example - Numeric Comparison

C 语言实例C Language Examples

Compare two numbers

In the following example, two integer variables are defined and compared using an if statement. You can first look at the logic diagram:

Example

#include <stdio.h> int main() { int a, b; a = 11; b = 99; // Alternatively, use the following code to let the user input two numbers in the terminal // printf("Enter first value:"); // scanf("%d", &a); // printf("Enter second value:"); // scanf("%d", &b); if(a > b) printf("a is greater than b"); else printf("a is less than or equal to b"); return 0; }

Output result:

a 小于等于 b

Compare three numbers

In the following example, two integer variables are defined and compared with three values using an if statement. You can first look at the logic diagram:

Example

#include <stdio.h> int main() { int a, b, c; a = 11; b = 22; c = 33; if ( a > b && a > c ) printf("%d is the largest", a); else if ( b > a && b > c ) printf("%d is the largest", b); else if ( c > a && c > b ) printf("%d is the largest", c); else printf("Two or three values are equal"); return 0; }

Output result:

33 最大

C Language Examples

other extensions