Exercise
Positive And Negative
Objective
Write a C# program to get a number and answer whether it is positive or negative.
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int number; // Declaring a variable to store the number entered by the user
// Asking the user to enter a number and reading the input
Console.Write("Enter a number: ");
number = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Checking if the number is positive, negative, or zero
if (number > 0) // If the number is greater than zero
{
Console.WriteLine("The number is positive."); // Printing that the number is positive
}
else if (number < 0) // If the number is less than zero
{
Console.WriteLine("The number is negative."); // Printing that the number is negative
}
else // If the number is neither positive nor negative (i.e., it is zero)
{
Console.WriteLine("The number is zero."); // Printing that the number is zero
}
}
}
More C# Programming Exercises of Flow Control
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and ...
In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...
In this C# exercise, you will learn how to modify the previous program using the else control structure. The program will ask the user for two numbers, and if the sec...
In this C# exercise, you will learn how to write a program that prompts the user to enter three numbers and displays the greatest one. The program will use conditional struc...
In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep as...
In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep re...