Positive And Negative - C# Programming Exercise

In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prompt the user to input a number, and then check if the number is greater than zero, less than zero, or equal to zero. The result will be a response indicating whether the number is positive or negative, or if it is zero. This type of exercise is excellent for practicing the use of control structures like if statements and handling user input in a console program in C#.

By completing this exercise, you will enhance your skills with conditional decision-making, a fundamental concept in programming that allows programs to respond dynamically to user input.

 Category

Flow Control

 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

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
        }
    }
}

 Share this C# exercise

 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#.

  •  Multiply if not zero

    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 ...

  •  Divide if not zero

    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...

  •  Divide if not zero (Using else)

    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...

  •  Greatest of three numbers

    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...

  •  Repeat until 0

    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...

  •  Repeat until 0 (Use Do While)

    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...