Compare Three integers in C



Comparing three integer variables is one of the simplest program you can write at ease. In this program, you can either take input from user using scanf() function or statically define in the program itself.

We expect it to be a simple program for you as well. We compare one value to rest of two and check the result and the same process is applied for all variables. For this program, all values should be distinct (unique).

Algorithm

Let's first see what should be the step-by-step procedure to compare three integers −

START
   Step 1 → Take two integer variables, say A, B& C
   Step 2 → Assign values to variables
   Step 3 → If A is greater than B & C, Display A is largest value
   Step 4 → If B is greater than A & C, Display B is largest value
   Step 5 → If C is greater than A & B, Display A is largest value
   Step 6 → Otherwise, Display A, B & C are not unique values
STOP

Flow Diagram

We can draw a flow diagram for this program as given below −

Three comparison flowchart

This diagram shows three if-else-if and one else comparitive statement.

Pseudocode

Let's now see the pseudocode of this algorithm −

procedure compare(A, B, C)

   IF A is greater than B AND A is greater than C
      DISPLAY "A is the largest."
   ELSE IF B is greater than A AND A is greater than C
      DISPLAY "B is the largest."
   ELSE IF C is greater than A AND A is greater than B
      DISPLAY "C is the largest."
   ELSE
      DISPLAY "Values not unique."
   END IF

end procedure

Implementation

Now, we shall see the actual implementation of the program −

#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("Values are not unique");

   return 0;
}

Output

Output of this program should be −

33 is the largest.
simple_programs_in_c.htm
Advertisements