Skip to content

calculator[Mercado]#58

Open
zero123-max wants to merge 2 commits intoapplication-development-08664:mainfrom
zero123-max:main
Open

calculator[Mercado]#58
zero123-max wants to merge 2 commits intoapplication-development-08664:mainfrom
zero123-max:main

Conversation

@zero123-max
Copy link
Copy Markdown

using System;

class Calculator
{
static void Main(string[] args)
{
double num1, num2, result;
string operation;

    // Method to get a valid number from user
    num1 = GetValidNumber("Enter first number:");
    operation = GetValidOperation();
    num2 = GetValidNumber("Enter second number:");

    // Perform calculation based on operation
    switch (operation)
    {
        case "+":
            result = num1 + num2;
            Console.WriteLine($"Result: {num1} + {num2} = {result}");
            break;
        case "-":
            result = num1 - num2;
            Console.WriteLine($"Result: {num1} - {num2} = {result}");
            break;
        case "*":
            result = num1 * num2;
            Console.WriteLine($"Result: {num1} * {num2} = {result}");
            break;
        case "/":
            if (num2 != 0)
            {
                result = num1 / num2;
                Console.WriteLine($"Result: {num1} / {num2} = {result}");
            }
            else
            {
                Console.WriteLine("Error: Division by zero is not allowed.");
            }
            break;
        default:
            Console.WriteLine("Invalid operation entered.");
            break;
    }
}

// Helper function to ensure valid number input
static double GetValidNumber(string prompt)
{
    double number;
    while (true)
    {
        Console.WriteLine(prompt);
        string input = Console.ReadLine();
        if (double.TryParse(input, out number))
        {
            return number;
        }
        else
        {
            Console.WriteLine("Invalid input. Please enter a valid number.");
        }
    }
}

// Helper function to ensure valid operation input
static string GetValidOperation()
{
    string operation;
    while (true)
    {
        Console.WriteLine("Enter an operation (+, -, *, /):");
        operation = Console.ReadLine();
        if (operation == "+" || operation == "-" || operation == "*" || operation == "/")
        {
            return operation;
        }
        else
        {
            Console.WriteLine("Invalid operation. Please enter +, -, *, or /.");
        }
    }
}

}

@zero123-max zero123-max changed the title calculator(Mercado) calculator[Mercado] Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant