Exercise
One Or Two Negative Numbers
Objective
Write a C# program to prompt the user for two numbers and determine whether both are negative, only one is negative, or neither is negative.
Example C# Exercise
Show C# Code
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
}
// Checking if only the first number is negative
else if (number1 < 0) // If the first number is less than 0
{
// Displaying a message if only the first number is negative
Console.WriteLine("Only the first number is negative."); // Informing the user that only the first number is negative
}
// Checking if only the second number is negative
else if (number2 < 0) // If the second number is less than 0
{
// Displaying a message if only the second number is negative
Console.WriteLine("Only the second number is negative."); // Informing the user that only the second number is negative
}
else
{
// Displaying a message if neither number is negative
Console.WriteLine("Neither number is negative."); // Informing the user that neither of the numbers is negative
}
}
}
Output
Case 1:
Enter the first number: -3
Enter the second number: -5
Both numbers are negative.
Case 2:
Enter the first number: 4
Enter the second number: -2
Only the first number is negative.
Case 3:
Enter the first number: 0
Enter the second number: -10
Only the second number is negative.
Case 4:
Enter the first number: 3
Enter the second number: 2
Neither number is negative.
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 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...
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...
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 ...
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...
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...
This C# exercise aims to create a program that displays multiplication tables from 2 to 6 using nested do...while loops. A do...while loop executes a block of ...