Positive or Negative
Below is the complete code for the Positive or Negative Try It Out! problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PositiveOrNegative
{
class Program
{
static void Main(string[] args)
{
// Ask the user for two numbers, converting them
// both to ints.
Console.Write("Enter number 1: ");
string input = Console.ReadLine();
int number1 = Convert.ToInt32(input);
Console.Write("Enter number 2: ");
input = Console.ReadLine(); // We'll just reuse this variable.
int number2 = Convert.ToInt32(input);
if (number1 == 0 || number2 == 0)
{
Console.WriteLine("The result won't be positive or negative.");
Console.WriteLine(
"One of the factors is 0, so the product will be 0 as well.");
}
else
{
// Figure out if each of the numbers, individually, are positive.
bool number1Positive;
bool number2Positive;
if (number1 > 0) { number1Positive = true; }
else { number1Positive = false; }
if (number2 > 0) { number2Positive = true; }
else { number2Positive = false; }
// If they have the same sign (both positive, or both negative)
// then print out that the result will be positive.
if ((number1Positive && number2Positive) ||
(!number1Positive && !number2Positive))
{
Console.WriteLine("The product will be positive.");
}
else // Otherwise, they have different signs, and be negative.
{
Console.WriteLine("The product will be negative.");
}
}
// Wait for the user to respond before closing...
Console.ReadKey();
}
}
}