Check if a Number is Greater than Another Number in CSharp

Check if a Number is Greater than Another Number in C#


Introduction of the Lesson

In programming, we often need to compare values and make decisions based on the results. For example, you might want to know which student scored higher on a test, or which number is larger when sorting data. In C#, relational operators make these comparisons possible.

One of the most common comparisons is checking if a number is greater than another number using the greater than ( > ) operator. This operator returns a Boolean value (true or false), which can then be used in conditions, decisions, or output messages.

In this lesson, you will learn how to write a simple C# program that asks the user to enter two numbers and checks if the first number is greater than the second. This foundational concept helps you understand how computers evaluate relationships between numbers and lays the groundwork for more complex decision-making programs.


Objectives (OBE-Oriented)

Before coding, let’s establish what we aim to achieve. These objectives focus on understanding, learning, practicing, and applying the concept.

  • Understand: Explain how relational operators work in C#, particularly the greater than operator (>).
  • Learn: Identify the correct syntax for comparing two values in C#.
  • Practice: Write and test a C# program that compares two numbers entered by the user.
  • Apply: Extend the concept by designing programs that use relational operators in real-world scenarios such as grading, ranking, or validation.

These objectives ensure you gain both conceptual knowledge and practical programming skills.


Beginner-Friendly Source Code & Instructions

Step-by-Step Instructions

  1. Open Visual Studio Code (ensure .NET SDK is installed).
    dotnet --version
    

    If a version number appears, you’re ready.

  2. Create a new console project in your course folder.
    dotnet new console -n number_comparison
    cd number_comparison
    
  3. Replace Program.cs with the following code.

Program.cs

using System;

namespace NumberComparisonApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Number Comparison Program ===");

            // Step 1: Get first number
            Console.Write("Enter the first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());

            // Step 2: Get second number
            Console.Write("Enter the second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());

            // Step 3: Compare numbers
            if (num1 > num2)
            {
                Console.WriteLine($"{num1} is greater than {num2}");
            }
            else if (num1 < num2)
            {
                Console.WriteLine($"{num1} is less than {num2}");
            }
            else
            {
                Console.WriteLine($"{num1} and {num2} are equal.");
            }
        }
    }
}

Example Output

=== Number Comparison Program ===
Enter the first number: 15
Enter the second number: 8
15 is greater than 8

Another example:

=== Number Comparison Program ===
Enter the first number: 12
Enter the second number: 20
12 is less than 20

If both are the same:

=== Number Comparison Program ===
Enter the first number: 10
Enter the second number: 10
10 and 10 are equal.

Explanation of the Program

  1. We begin by importing the System namespace, which allows us to use Console.WriteLine and Console.ReadLine.
  2. Inside the Main method, the user is asked to input two integers. These are converted from text (string) into integers (int) using Convert.ToInt32().
  3. Using relational operators:
    • > checks if the first number is greater than the second.
    • < checks if the first number is less than the second.
    • If neither is true, then the numbers are equal.
  4. The program then outputs a clear result for the comparison.

This simple logic shows how computers make decisions based on conditions, a crucial skill for programming real-world applications.


Summary

In this lesson, you learned how to use relational operators in C# to compare two numbers. Specifically, you created a program that checks if one number is greater, less than, or equal to another number. This builds your foundation for decision-making in programs, teaching you how Boolean results from comparisons can guide program flow. Whether for ranking scores, validating inputs, or building interactive apps, comparisons are everywhere in programming.


5-Item Multiple Choice (Bloom’s Taxonomy)

  1. Which operator is used in C# to check if one value is greater than another?
    • A. >=
    • B. >
    • C. <
    • D. =
  2. If num1 = 15 and num2 = 10, what will the condition (num1 > num2) return?
    • A. false
    • B. true
    • C. error
    • D. undefined
  3. Which C# method is used to convert string input into an integer?
    • A. int.Parse()
    • B. Convert.ToInt32()
    • C. Console.WriteLine()
    • D. ToString()
  4. What output will the program display if num1 = 5 and num2 = 5?
    • A. 5 is greater than 5
    • B. 5 is less than 5
    • C. 5 and 5 are equal
    • D. Error
  5. Why are relational operators important in programming?
    • A. They allow adding numbers
    • B. They are used for comparisons
    • C. They display output
    • D. They convert types

Exercises, Assessment, and Lab Exam

Introduction to Exercises

Now that you have learned how to compare two numbers, it’s time to strengthen your skills with hands-on practice. These activities will help you go beyond simple comparisons and apply relational operators in meaningful ways.


Exercises

  1. Modify the program to display the larger of the two numbers only.
  2. Add an option to compare three numbers and display the greatest.
  3. Ask the user for their score and check if it is greater than or equal to 75 (passing mark).
  4. Write a program that checks if the entered number is greater than 100.
  5. Extend the program to say if a number is positive, negative, or zero.

Assessment

Performance Task:
Write a program that asks the user for two numbers and:

  • Displays which is greater, or if they are equal.
  • Uses relational operators for the comparison.
  • Handles negative numbers correctly.

Lab Exam

Instruction:
Create a C# console application that:

  1. Accepts two integers from the user.
  2. Compares the numbers using relational operators.
  3. Displays the result (greater, less, or equal).
  4. Prints “Invalid input” if the user types a non-numeric value.

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation