% MATLAB Basic Code Example: Sum of Two Numbers

% Define two numbers
number1 = 5;
number2 = 7;

% Calculate the sum
sumResult = number1 + number2;

% Display the result
fprintf('The sum of %d and %d is: %d\n', number1, number2, sumResult);


  1. Define Numbers: We start by defining two numbers (number1 and number2).

  2. Calculate Sum: Using the + operator, we calculate the sum of number1 and number2 and store the result in the variable sumResult.

  3. Display Result: The fprintf function is used to display the result. It prints a formatted message showing the numbers and their sum.

  4. Run the Code: Save the script with a .m extension (e.g., sum_example.m) and run it in MATLAB.

  5. Result:

The sum of 5 and 7 is: 12