settingsAccountsettings
Menusettings

Q: Find out the multiplication sign of 3 numbers without calculating it in C#

+3 votes

I need to write a program that shows the sign (+, - or 0) of the product (multiplication) of three (3) real numbers, without calculating it.

I must use a sequence of if operators.

Examples:

a

b

c

result

5

2

2

+

-2

-2

1

+

-2

4

3

-

0

-2.5

4

0

-1

-0.5

-5.1

-

 

asked in C# category by user sam

2 Answers

+1 vote

Here is my solution. Although, a bit long...

using System;

class MultiplicationSign
{
    static void Main()
    {
        Console.WriteLine("Please enter 3 numbers:");
        double a = double.Parse(Console.ReadLine());
        double b = double.Parse(Console.ReadLine());
        double c = double.Parse(Console.ReadLine());

        if ((a < 0 && b > 0 && c > 0) || (b < 0 && a > 0 && c > 0) || (c < 0 && a > 0 && b > 0) || (a < 0 && b < 0 && c < 0))
        {
            Console.WriteLine("-");
        }
        else if ((a < 0 && b < 0 && c > 0) || (a < 0 && c < 0 && b > 0) || (b < 0 && c < 0 && a > 0) || (a > 0 && b > 0 && c > 0))
        {
            Console.WriteLine("+");
        }
        else if (a == 0 || b == 0 || c == 0)
        {
            Console.WriteLine("0");
        }
    }
}
answered by user john7
edited by user golearnweb
+1 vote

 Another result:       

if (a > 0 && b > 0 && c > 0)
            {
                Console.WriteLine("+");
            }
            else if (a < 0 && b < 0 && c < 0)
            {
                Console.WriteLine("-");
            }
            else if (a > 0 && b > 0 && c < 0)
            {
                Console.WriteLine("-");
            }
            else if (a > 0 && b < 0 && c > 0)
            {
                Console.WriteLine("-");
            }
            else if (a < 0 && b > 0 && c > 0)
            {
                Console.WriteLine("-");
            }
            else if (a > 0 && b < 0 && c < 0)
            {
                Console.WriteLine("+");
            }
            else if (a < 0 && b < 0 && c > 0)
            {
                Console.WriteLine("+");
            }
            else if (a < 0 && b > 0 && c < 0)
            {
                Console.WriteLine("+");
            }
            else
            {
                Console.WriteLine(0);
            }
answered by user eiorgert
edited by user golearnweb
...