Alright so in the book im reading its telling me to write a program that asks the user for two multiplication numbers and then it will print out whether the result should be positive or negative.
The rule is that i cannot take two numbers and multiply them, and then check if the result is greater than or less than zero. Instead if the two numbers have the same sign (both positive or both negative) the result is positive. If they have different signs, the result is negative.
I did it like this ( also i added int anresult = first * second; <- do i need that?) because if i remove it it still works, but the book wants it in multiplication. so im not sure if that piece of code is doing anything at all
Console.WriteLine("Enter First Nuumber: ");
string firstAsString = Console.ReadLine();
int first = Convert.ToInt32(firstAsString);
Console.WriteLine("Enter Second Nuumber: ");
string secondAsString = Console.ReadLine();
int second = Convert.ToInt32(secondAsString);
int anresult = first * second;
if (first >= 0 && second > 0 || first < 0 && second < 0)
Console.WriteLine("Your number will be Positive");
else
Console.WriteLine("Your number will be negative");
Console.ReadLine();
But the book did it like this
Console.WriteLine("Enter First Nuumber: ");
string firstAsString = Console.ReadLine();
int first = Convert.ToInt32(firstAsString);
Console.WriteLine("Enter Second Nuumber: ");
string secondAsString = Console.ReadLine();
int second = Convert.ToInt32(secondAsString);
bool firsNumberPositive;
bool secondNumberPositive;
if (first > 0) {
firsNumberPositive = true;
}
else
{
firsNumberPositive = false;
}
if (second > 0)
{
secondNumberPositive = true;
}
else
{
secondNumberPositive = false;
}
if (firsNumberPositive && secondNumberPositive || !firsNumberPositive && !secondNumberPositive)
Console.WriteLine("Answer Is Positive");
else
if (!firsNumberPositive && secondNumberPositive || firsNumberPositive && !secondNumberPositive)
Console.WriteLine("Answer Is Negative");
Console.ReadLine();
isnt my way the same as the books way? because the book shows
if (first > 0) {
firsNumberPositive = true;
}
which is using the numbers just like i am to display positive and negative
heres mine, same thing?..
if (first >= 0 && second > 0 || first < 0 && second < 0)
Console.WriteLine("Your number will be Positive");
else
Console.WriteLine("Your number will be negative");
bool firstNumberPositive = first > 0;
. Any time you seeif
branches assigning (or returning) true and false without doing anything else, it can be improved.first < 0 ^ second >= 0
Ignore the book.