Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get my program to correctly display decimal numbers


using System.Globalization;

namespace MathOperations
{
    class Program
    {
        static void Main(string[] args)
        {
            bool use_again = true;
            Console.WriteLine("Welcome to my own calculator program");
            while (use_again == true)
            {
                Console.WriteLine();
                Console.WriteLine("Please input the first number : ");
                double number1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please input the second number : ");
                double number2 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine();
                Console.WriteLine("Please input an option");
                Console.WriteLine("Add : +");
                Console.WriteLine("Substract : -");
                Console.WriteLine("Multiply : *");
                Console.WriteLine("Devide : /");
                char operation_symb = Convert.ToChar(Console.ReadLine());
                double result = 0;

                switch (operation_symb)
                {
                    case '+':
                        result = Math.add(number1, number2);

                        break;
                    case '-':
                        result = Math.Substract(number1, number2);

                        break;
                    case '*':
                        result = Math.Multiply(number1, number2);

                        break;

                    case '/':
                        while (number2 == 0)
                        {
                            Console.WriteLine("You can't devide by zero idiot");
                            Console.WriteLine("Please input a valid option");
                            number2 = Convert.ToDouble(Console.ReadLine());
                            if (number2 == 0)
                            {
                                continue;
                            }
                            result = Math.Devide(number1, number2);
                        }
                        break;
                    default:
                        Console.WriteLine("That's not a valid option");
                        break;

                }

                Console.WriteLine($"The result is {number1} {operation_symb} {number2} = {result.ToString("F10", CultureInfo.CurrentCulture)}");
                Console.WriteLine("Would you like to use the calculator again?(Y/N)");
                string answer;
                answer = Console.ReadLine().ToUpper();
                answer = "";
                switch (answer)
                {
                    case "Y":
                        use_again = true;
                        break;
                    case "N":
                        use_again = false;
                        Console.WriteLine("Thanks for using me");
                        Environment.Exit(0);
                        break;
                    default:

                        Console.WriteLine("That's not a vailid opetion");
                        answer = Console.ReadLine().ToUpper();

                        if (answer == "Y")
                        {
                            use_again = true;

                        }
                        else if (answer == "N")
                        {
                            use_again = false;

                        }
                        break;
                }

            }


        }


    }

}
class Math
{
    public static double add(double number1, double number2)
    {
        return number1 + number2;
    }
    public static double Substract(double number1, double number2)
    {
        return number1 - number2;
    }
    public static double Multiply(double number1, double number2)
    {
        return number1 * number2;
    }
    public static double Devide(double number1, double number2)
    {
        return number1 / number2;
    }
}








I am a beginner in C# and I have made this calculator program but when I for example devide 1 by 2 the output will be 0.0000000000. What do you think is the problem with the above code. For the other operations the decimal number work properly. Btw can you not use ChatGPT to answer this question please

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You get a output value of 0 because the while statement in the switch case return false if number2 is not 0.

Change your devide switch case with that :

case '/':
    while(number2 == 0){
       Console.WriteLine("You can't devide by zero idiot");
       Console.WriteLine("Please input a valid option");
       number2 = Convert.ToDouble(Console.ReadLine());
    }
    result = Math.Devide((double)number1, (double)number2);
break;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading