programme a calcualtor in matlab

main.m
% Program to create a simple calculator in MATLAB
% Prompt the user for operation choice and two numbers

% Display a message to the user to enter the desired operation
disp('Choose an operation:');
disp('1: Addition');
disp('2: Subtraction');
disp('3: Multiplication');
disp('4: Division');

% Prompt the user to enter the desired operation
operation = input('Enter operation choice (1-4): ');

% Prompt the user to enter the first number
num1 = input('Enter the first number: ');

% Prompt the user to enter the second number
num2 = input('Enter the second number: ');

% Use a switch statement to perform the desired operation
switch operation
    case 1 % Addition
        result = num1 + num2;
        disp(['Result: ' num2str(result)]);
    case 2 % Subtraction
        result = num1 - num2;
        disp(['Result: ' num2str(result)]);
    case 3 % Multiplication
        result = num1 * num2;
        disp(['Result: ' num2str(result)]);
    case 4 % Division
        % Use a conditional statement to check for division by zero
        if num2 == 0
            disp('Cannot divide by zero');
        else
            result = num1 / num2;
            disp(['Result: ' num2str(result)]);
        end
    otherwise % Invalid operation
        disp('Invalid operation choice');
end
1285 chars
42 lines

This program prompts the user to choose an operation (addition, subtraction, multiplication, or division) and then prompts the user to enter two numbers. A switch statement is used to perform the chosen operation and the result is displayed to the user. A conditional statement is used to check for division by zero.

gistlibby LogSnag