C Programming Examples – Comparing 2 numbers – Simple If Else if Else problem


Difficulty: Beginner

Problem: Create a program that will ask a user to enter 2 numbers. Determine if the first number is greater than, less than, or equal to the second number.

VN:F [1.9.11_1134]
Rating: 5.0/5 (2 votes cast)

Example Output 1

Enter 1st num: 2.5
Enter 2nd num: 2.4

The 1st number is greater than 2nd number.


Example Output 2

Enter 1st num: 3
Enter 2nd num: 3.1

The 1st number is less than 2nd number.

Example Output 3

Enter 1st num: 3
Enter 2nd num: 3.0

The 1st number is equal to the 2nd number.

Determine equality of 2 numbers

Figure 1.0 - Sample Output


Solution:

How can you say that a number is greater than another number? It’s by comparing them. Then after comparing them we determine if the 1st number is more, less, or if it is equal. By creating conditions we can we can achieve this. Let’s list down the step by step procedure.

  1. We first accept the 2 numbers.
  2. Then compare the first number to the second number.
  3. We determine what to print out using the following conditions:
  • If 1st number is greater than the 2nd number print:
The 1st number is greater than 2nd number.
  • If 1st number is lesser than the 2nd number print:
The 1st number is lesser than 2nd number.
  • If 1st number is equal to 2nd number print:
The 1st number is equal to 2nd number.

Let us now translate this steps into source code.

Let us declare first the 2 variables as num1 and num2.

float num1, num2;

Why I have used float? It is because our example output shows that the user can enter numbers with fractions.

The next thing we have to do after the user placed the 2 values is do the condition. We can first try if num1 is greater than num2.

if(num1 < num2)
     printf("The 1st number is greater than 2nd number.");
else if(num1 &lt; num2)
     printf("nThe 1st number is less than 2nd number.");
else
     printf("nThe 1st number is equal to the 2nd number.");

The Final Source Code:

#include<stdio.h>
 
main()
{        
        //Declare 2 variables for the 2 numbers
        float num1, num2;
        //ask the user to enter the number and then store
        //the value to respective variables.
        printf("Enter 1st num: ");
        scanf("%f", &num1);
 
        printf("Enter 2nd num: ");
        scanf("%f", &num2);
 
        //This is where the condition comes in.
        //Exactly the same as how we say it it English
        //Step by Step
        if(num1 > num2)
            //After the condition is met lets print this,
            //if not proceed to next condition
            printf("nThe 1st number is greater than 2nd number.");
        else if(num1 < num2)
            printf("nThe 1st number is less than 2nd number.");
        else
            printf("nThe 1st number is equal to the 2nd number.");
 
        getch();
}

C Programming Examples – Comparing 2 numbers – Simple If Else if Else problem, 5.0 out of 5 based on 2 ratings

Related posts:

  1. C Programming Examples – Displaying Even Numbers using Loop Statement
  2. C Programming Examples – Simple Averaging
  3. C Programming Examples – Fibonacci Series Using Loop Statement
  4. C Programming Examples – Factorial using Loop Statement
  5. C Programming Examples – Averaging Using Loop Statement

If you have any questions please feel free to discuss it on our discussion board

www.itechsociety.com/forum/topic/c-lesson-4-%E2%80%93-accepting-input-1

This entry was posted in C/C#/C++, Examples, Programming, Tutorials and tagged , , . Bookmark the permalink.
This article is written by: chicoi08
I love making great things using my computer. From programming to designing to blogging. I enjoy doing them. I hope this website can help some people out there. View all posts by chicoi08 →

Discussion 9 Comments

  1. Jonathan says:

    nice kieth…..all problems na bah 2…

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
    • tiger says:

      #include
      using namespace std;

      int main()
      {
      double a, b;
      // get and enter the numbers
      cout <> a;
      cout <> b;

      // conditional operator used to determine the
      //size of each number

      if(a > b ? a == b : a < b)

      cout << "First number is smaller than the second.n";

      else if(a==b)// just in case both numbers entered are equal

      cout<<"Both numbers are the same.n";
      else

      cout << "First number is larger than the second.n";

      system("pause");
      return 0;

      }

      VA:F [1.9.11_1134]
      Rating: 0 (from 0 votes)
  2. oh keith i have one just like that but in this If else If else statement, the program will ask the user to input three numbers. And when this numbers are input in the program, the if else if else statement will work.
    this is example!

    #include
    #include
    int a, b, c;
    int main() {
    printf(” Enter three numbers: “);
    scanf(“%d%d%d”,&a,&b,&c);
    if(a>b)
    if(a>c)
    printf(“n %d is the longest”,a);
    else
    printf(“n %d is the longest”,c);
    else
    if(b>c)
    printf(“n %d is the longest”,b);
    else
    printf(“n %d is the longest”,c);
    getch();
    }

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
    • chicoi08 says:

      Hi Dexter. Nice example. It does the job, but it is not the right way of writing the program.

      1. Using multiple variables in 1 scanf statement is not good. Because considering the “User Friendliness”, users want to see what actually is happening. If you do this, the actual program will look like disarranged.

      printf(" Enter 1st number: ");
      scanf("%d",&a);
      printf(" Enter 2nd number: ");
      scanf("%d",&b);
      printf(" Enter 3rd number: ");
      scanf("%d",&c);

      2. You have used if statements in a not so nice way too. It’s because the “neatness and arrangement” is not on standard. You can write the program like this. I bet you can understand this better.

      if ((a > b) && (a > c))
          printf(“n%d is the longest”,a);
      else if ((b > a) && (b > c))
          printf(“n%d is the longest”,b);
      else if ((c > a) && (c > b))
          printf(“n%d is the longest”,c);
      else
          printf(“nNeither of them is greater.”,c);
      VN:F [1.9.11_1134]
      Rating: 0 (from 0 votes)
      • hmm. Sorry but that statement of mine that i learned came from sir Kevin. I only apply some of it into my own statement. In this case, the one you have there is very nice too. Its very understandable and neat. But lately i understand that i just work for a simple statement or simply i just make a short cut on the 1st printf statement where i use to enter three numbers not making the user to press enter again for determining the 2nd and the third number. ( ok lapit na nose bleed hehehe )

        Keith, i just want to defend my statement. Because that is what a programmers must do.

        Ayokong kontrahin ka, alam ko na tama ka but for me im just a beginner and i want to learn more. Thanks for the help in correcting my mistakes. Pero hanggang my idedefend ako gagawin ko. ^^ peace

        VA:F [1.9.11_1134]
        Rating: 0 (from 0 votes)
      • chicoi08 says:
        not making the user to press enter again for
        determining the 2nd and the third number.

        Have you tried this on a compiler?
        Because I tried it and I still need to press enter 3 times.

        Try it.

        I’ll post a new article soon on how to properly write a program.

        Although Defending your code is just right ^^

        One more thing. I Suggest to use our Discussion board. It is specially created for longer topics. Peace

        VN:F [1.9.11_1134]
        Rating: 0 (from 0 votes)
  3. those include are stdio.h and conio.h ^^

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
  4. Hahaha!! kahit nga space bar lng between those numbers eh! ahh bztah!

    I know how to write a proper program. It is that i just want to make it in a simple way!

    Begginer pa lng ako. And there are many things that i must learn.
    hehehe!! cge gamit na ako ng discussion board! ^^

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
  5. aquina says:

    its very nice and we can understand it easyly.ssssssssssssooooooooooooooooooooooooooo nice

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">