Easy Tutorial
❮ C Exercise Example7 C Function Vsprintf ❯

C Language Examples - Numerical Comparison

C Language Examples

Comparing Two Numbers

In the following example, two integer variables are defined, and the if statement is used to compare the two values. You can first look at the logic diagram:

Example

#include <stdio.h>

int main() {
   int a, b;

   a = 11;
   b = 99;

   // Alternatively, you can allow the user to input two numbers in the terminal
   // printf("Enter the first value:");
   // scanf("%d", &a);
   // printf("Enter the 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 is less than or equal to b

Comparing Three Numbers

In the following example, three integer variables are defined, and the if statement is used to compare the three values. 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 is the largest

C Language Examples

❮ C Exercise Example7 C Function Vsprintf ❯