C Program for Comparing Two Numbers

Today we are going to discuss yet another basic and simple program. C Program for comparing two numbers.

This particular coding question is usually asked as interview questions. Thus, it becomes our top priority to learn this programming question in depth. 

Operators we will use:

Just like our previous articles, program for addition of two matrix in c++, and c++ program for area of square, this problem also has multiple approaches for solving. But for this post, we are going to look into two of these approaches. Since, they will involve two different kinds of operators. 

To understand these two methods better, let us go in depth of the two operators which we will use. 

Relational Operators:

As discussed earlier, relational operators are used when we need to compare two operands or check their relationships. Since our program is about comparing two numbers, it is fitting that we use relational operators. 

The different relational operators are: 

==It will check if the values of two operands are equal or not. If yes, then the condition becomes true.
!=This operator checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.
>Next we have greater than operator which checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.
<Similarly we have less than which checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.
>=Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.
<=Similarly, this checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.
Relational Operators.

Ternary Operator:

One of the most compact operators in C are ternary operators. These may have only two symbols involved but we can perform most of the complex operations through ternary operators. 

They basically have two modes and they take three parameters.

The first, comparison argument.

Second parameter for the true comparison.

Third argument for the result of false comparison.

The syntax of the ternary operator is : condition ? value_if_true : value_if_false.

For instance, (9>0)? Yes : No;

The above expression will give the result yes since the statement 9>0 is true. But if we reverse the statement as 0>9, the result will be no since the expression is false. 

C Programs for Comparing Two Numbers

You might have guessed that for this program the two approaches will be one with Relational operator and other with ternary operator. Thus, you will have two ways of solving this problem in your interview.

Hence, without wasting much time, let’s begin:

C Program for Comparing Two Numbers Using Relational Operators and If-else:

Algorithm:

  1. Start.
  2. Declare two integers a and b for the two numbers we have to compare.
  3. Then, take user input for the two numbers.
  4. If a>b then print a is greater than b.
  5. Else if a = b then print a is equal to b.
  6. Else, print a is smaller than b.
  7. Finally, the result is displayed according to the inputs.
  8. Stop.

Program:

//C Program for Comparing Two Numbers Using Relational Operators and If-else
#include <stdio.h>
int main()
{
    int a;
    int b;
    printf("Enter first number : ");
    scanf("%d", &a);
    printf("Enter second number : ");
    scanf("%d", &b);
    if (a > b)
    {
        printf("%d is greater than %d\n", a, b);
    }
    else if (a==b)
    {
        printf("%d is equal to %d\n", a, b);
    }
    else
    {
        printf("%d is smaller than %d\n", a, b);
    }
}

Program Explanation:

At first we have declared two integer variables ‘a’ and ‘b’ for the two numbers we have to compare.

After that we ask the user to input these two numbers.

Then, using if else statements we compare the two numbers and print the result.

Thus, you can see how simple this approach is. The only problem with this code is, it is lengthy. We can make it shorter and more efficient using a ternary operator. 

Output:

This is what we get as a result of the code above :

Output C Program for Comparing Two Numbers Using Relational Operators and If-else
Output C Program for Comparing Two Numbers Using Relational Operators and If-else

 

C Program for Comparing Two Numbers Using Ternary Operator:

This program will use a ternary operator for comparing the two numbers. Additionally, it will be a compact code and will be more efficient and less power consuming. 

To have an overview, let us start with the algorithm of this program.

Algorithm:

  1. Start.
  2. Declare integer variables a and b for two numbers.
  3. Then, enter the two integers from the user using scanf.
  4. After that, define the ternary operator as follows.
  5. If a != b, then move to another ternary operator inside the parameter for true condition.
  6. Inside the nested ternary operator, if a>b, then print a is greater than b.
  7.  If the condition is false then print a is smaller than b.
  8. At last, in the outer ternary operator for the false condition, print a is equal to b.
  9. Stop.

Program:

//C Program for Comparing Two Numbers Using Ternary Operator
#include <stdio.h>
int main()
{
    int a;
    int b;
    printf("Enter first number : ");
    scanf("%d", &a);
    printf("Enter second number : ");
    scanf("%d", &b);
   ( a != b )? (a>b ? printf("%d is greater than %d\n", a, b)
    : printf("%d is smaller than %d\n", a, b) )
    : printf("%d is equal to %d\n", a, b) ;
}

Program Explanation:

Though it is a compact program, it can be confusing. As you might have noticed, the above program uses nested ternary operators. Thus, let us go through the conditions step by step for better understanding.

Similar to the previous approach, we first define the main function to declare two integer variables.

Then, we enter the numbers we wish to compare using user input.

After that we define the ternary operator. 

The first condition is to check is a is not equal to b. If this condition is true, then we have another ternary operator inside it. Then, if  a > b then, we print a is greater than b. If this condition fails, then print a is smaller than b. 

Now we come out of the nested ternary operator. On the outside if the condition a != b fails, we print a is equal to b. 

Therefore, we print the result according to the inputs. 

Output:

This is what we get as a result of the code above : 

Output of C Program for Comparing Two Numbers Using Ternary Operator
Output of C Program for Comparing Two Numbers Using Ternary Operator.

Modifications:

Some modifications that you can try in this program:

  1. Using float numbers for comparison.
  2. You can also compare numbers using structures. 
  3. Another efficient method of comparing two numbers is to use pointers. 
  4. Additionally, you can also use strings for comparison. 

Summing it all up:

In conclusion, this program for comparing two numbers adds up to the number of basic programming questions but it is important. Because it involves the most important concepts of programming like decision making and operators.

Thus, you can get thorough knowledge about these necessary concepts through this simple program. 

 In other words, the c program for comparing two numbers is a very efficient and useful program. It is a must learn code for beginners. 

Don’t forget to modify your program and find your own code. This will boost your skills. 

See you in our next post!

Leave a Comment

Your email address will not be published. Required fields are marked *