Check if a Number is Positive, Negative, or Zero
Table of Contents
- Check if a Number is Positive, Negative, or Zero
- Introduction
- Objectives
- Beginner-Friendly Source Code and Step-by-Step Instructions
- Step-by-Step Instructions
- C# Program: Check if a Number is Positive, Negative, or Zero
- How to Run
- Example Output
- Explanation of the Program
- Summary
- Quiz (5 Items – Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introduction to Exercises
- Exercises
- Assessment
- Lab Exam
Introduction
In this lesson, we will explore how to use conditional statements in C# to determine whether a number is positive, negative, or zero. Conditional statements allow your program to make decisions based on certain conditions, similar to how humans make choices depending on situations. This topic is a great opportunity to apply logical thinking while learning how to control the flow of your program. You’ll discover how simple comparisons can help programs react differently to various inputs, an essential skill for all programmers.
Objectives
Before you begin coding, it’s important to understand what you aim to achieve. This lesson focuses on developing logical reasoning and coding accuracy through conditional statements. The objectives are divided into four Outcome-Based Education (OBE) levels:
- Understand: Explain how if-else statements work to classify numbers based on their value.
- Learn: Identify the proper syntax for using if, else if, and else statements in C#.
- Practice: Write and run a C# console program that checks whether a number is positive, negative, or zero.
- Apply: Modify the code to handle additional scenarios, such as checking if the input is even, odd, or invalid.
These objectives encourage learners to analyze, design, and apply programming logic, preparing them for more advanced topics like nested conditions and decision trees.
Beginner-Friendly Source Code and Step-by-Step Instructions
Let’s create a simple program to determine whether a number is positive, negative, or zero.
Step-by-Step Instructions
- Open Visual Studio Code.
Ensure the .NET SDK is installed by typingdotnet --versionin the Terminal. - Create a project folder.
Example:D:\2025-1_doe_bsis_1a - Open the folder in VS Code.
Go to File → Open Folder… - Create a new console project.
In the terminal, type:dotnet new console -n number_check - Navigate to the project folder.
cd number_check - Open Program.cs and replace its contents with the code below.
C# Program: Check if a Number is Positive, Negative, or Zero
using System;
namespace NumberCheckApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
double number = Convert.ToDouble(Console.ReadLine());
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
}
How to Run
- Open the Terminal inside VS Code.
- Type:
dotnet run
Example Output
Enter a number: 10
The number is positive.
Another Example:
Enter a number: -5
The number is negative.
Another Example:
Enter a number: 0
The number is zero.
Explanation of the Program
- The program starts by importing the System namespace, which contains essential input and output functions.
- Inside the Main() method, the program asks the user to input a number using
Console.ReadLine(). - The input string is converted to a numeric type (
double) usingConvert.ToDouble()to handle both whole numbers and decimals. - The program uses if-else-if statements to check three conditions:
- If the number is greater than zero → it’s positive.
- If the number is less than zero → it’s negative.
- Otherwise → it’s zero.
- Each condition executes its respective
Console.WriteLine()statement to display the result.
This logic mirrors everyday decision-making, such as determining whether the temperature is hot, cold, or just right.
Summary
In this lesson, you learned how to use if-else statements in C# to check whether a number is positive, negative, or zero. This skill is a foundation for writing programs that react intelligently to user input. You now understand how to compare values, use relational operators, and structure conditional logic for decision-making. Mastering this concept prepares you for more complex real-world applications where programs must evaluate and respond to various conditions.
Quiz (5 Items – Bloom’s Taxonomy)
- What is the purpose of an if statement in C#?
A. To repeat a task
B. To make decisions based on conditions
C. To define a method
D. To display data - Which operator is used to check if a number is greater than zero?
A.<
B.==
C.>
D.<= - What will happen if the user inputs 0?
A. The program displays “The number is positive.”
B. The program displays “The number is zero.”
C. The program displays “The number is negative.”
D. The program gives an error. - What does
Convert.ToDouble()do in the program?
A. Converts text to lowercase
B. Converts a string input to a double value
C. Displays the number in decimal format
D. Checks if the number is valid - Why is the if-else structure useful in this program?
A. It makes the program run faster
B. It allows decisions based on input values
C. It repeats instructions automatically
D. It prints multiple lines at once
Exercises, Assessment, and Lab Exam
Introduction to Exercises
These exercises are designed to strengthen your understanding of conditional statements in C#. By completing them, you’ll develop logical thinking and improve your ability to write decision-based programs. Each task builds upon the core concept of evaluating numerical input and displaying results accordingly.
Exercises
- Modify the program to check if the number is even or odd.
- Create a program that checks if a temperature is below freezing (less than 0).
- Write a program that displays “Positive and Even” or “Positive and Odd.”
- Ask the user for two numbers and display which one is larger.
- Add input validation to handle invalid or non-numeric entries.
Assessment
- Quiz: Complete the 5-item multiple-choice test above.
- Performance Task: Write a C# program that checks if a person’s body temperature is normal (between 36°C and 37.5°C).
Lab Exam
Create a C# program that accepts a number and classifies it as:
- Positive and greater than 100
- Positive and less than or equal to 100
- Negative
- Zero
Your program must use if-else-if statements and display the correct message based on the input.
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.