Two Negative Numbers - C# Programming Exercise

In this C# exercise, you will learn how to write a program that prompts the user to enter two numbers and determines whether both numbers are negative or not. This type of program is essential for practicing conditional structures like if-else, which allow you to make decisions based on the input data. In this case, the program will check if both numbers are less than zero and provide a message indicating whether both numbers are negative or not. This exercise is perfect for understanding how to work with conditions and comparisons in C#, which is a crucial skill for creating interactive and dynamic programs that respond based on user input.

The exercise will help you understand how to compare numerical values and use logical operators to combine conditions, which is an important foundation for building more complex programs. Additionally, it will teach you how to use the if structure to evaluate multiple conditions and give clear feedback to the user about the results of the comparison.

 Category

Flow Control

 Exercise

Two Negative Numbers

 Objective

Write a C# program to prompt the user for two numbers and determine if both numbers are negative or not.

 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()
    {
        // Declaring two variables to store the numbers entered by the user
        int number1, number2;

        // Asking the user to enter the first number
        Console.Write("Enter the first number: ");
        number1 = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer

        // Asking the user to enter the second number
        Console.Write("Enter the second number: ");
        number2 = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer

        // Checking if both numbers are negative
        if (number1 < 0 && number2 < 0) // If both numbers are less than 0
        {
            // Displaying a message if both numbers are negative
            Console.WriteLine("Both numbers are negative."); // Informing the user that both numbers are negative
        }
        else
        {
            // Displaying a message if one or both numbers are not negative
            Console.WriteLine("Both numbers are not negative."); // Informing the user that one or both numbers are not negative
        }
    }
}

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

  •  One or two negative numbers

    In this C# exercise, you will learn how to write a program that prompts the user to enter two numbers and then determines whether both numbers are negative, only one is nega...

  •  Multiples

    In this exercise, you will learn how to use the modulo operator % to find numbers between 1 and 500 that are divisible by both 3 and 5. The program will...

  •  Number repeated

    This exercise teaches you how to display a number repeated a number of times specified by the user. Three loop structures will be used to perform the repetition: while, d...

  •  Password

    In this C# programming exercise, the user is asked to enter their login and password, both as integer numbers. The goal is to create a program that repeatedly ...

  •  Password V2

    This C# exercise aims to create a program that asks the user for their login and password (both must be integer numbers) and repeats the request until the ente...

  •  Many divisions

    This C# exercise aims to develop a program that asks the user for two numbers and displays the result of the division of these numbers along with the remainder of the divisi...