Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So im a beginner and i needed to create a calculator. The problem is i downt know how to make it so that if "4 / 0 = 0; 9/0= 0" and so on.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sav4
{
    class Program
    {
        static void Main(string[] args)
        {
            
            char x;
            double a;
            double b;
            double ats;
            Console.WriteLine("insert first number ");
            a = double.Parse(Console.ReadLine());
            Console.WriteLine("insert second number ");
            b = double.Parse(Console.ReadLine());
            Console.WriteLine("insert inc "); // - + * /
            x = (char)Console.Read();
            Console.Clear();
            if (x == '-' )
            {
                ats = a - b;
                Console.WriteLine("{0} - {1} = {2}", a, b, ats);
            }
            else
            {
                if (x == '+')
                {

                    ats = a + b;
                    Console.WriteLine("{0} + {1} = {2}", a, b, ats);
                }
                else
                {
                    if (x == '*')
                    {
                        ats = a * b;
                        Console.WriteLine("{0} * {1} = {2}", a, b, ats);
                    }
                    else
                    {
                        if (x == '/')
                        {
                            ats = a / b;
                            if (a/b != 0)
                            {
                                Console.WriteLine("{0} / {1} = {2}", a, b, ats);
                            }
                            else
                            {
                                Console.WriteLine("Lygybe neegzistuoja", a, b, ats);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error");
                        }
                        } }
                }
            }
            }

        }


What I have tried:

I guess thinking a lot thats it
Posted
Updated 15-Sep-18 0:30am

If b is zero, then trying to do this:
C#
ats = a / b;
will give you an exception because division by zero does not give you a "real world" number you can store in a double
Very definately, 0 is not the correct result!

So there are two ways to handle it:
1) Check before you divide! Put a quick test in before you try, and report a problem if it won't work instead of doing it.
2) Use a try...catch block to detect errors and handle them gracefully: try-catch (C# Reference) | Microsoft Docs[^]
 
Share this answer
 
Comments
Richard Deeming 18-Sep-18 16:22pm    
Actually, since it's using the double division operator, it won't produce an exception. The result will be either double.PositiveInfinity or double.NegativeInfinity, depending on the sign of a.
Hi there,

Following is the code to print zero if we are dividing any double by zero.
Suppose we have:
double a = 10, b = 0, c=0;
          try
          {
              c = double.IsInfinity(a / b) ? 0 : a / b;
          }
          finally
          {
              Console.WriteLine(c);
          }


Quote:
Here if we are using double, then we are not getting the exception "DivideByZeroException" then it just returns infinity. Double has also a method to check the infinity i.e. "double.IsInfinity()". Using this method you can check whether the result is infinity or not and you can take appropriate action.

In your case, the code will be :

arts=double.IsInfinity(a / b) ? 0 : a / b;


Thanks.
 
Share this answer
 
Comments
phil.o 15-Sep-18 6:51am    
Returning zero in case of a division by zero is mathematically wrong.
That's not because you can do it that you should do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900