% 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);
Define Numbers: We start by defining two numbers (
number1
andnumber2
).Calculate Sum: Using the
+
operator, we calculate the sum ofnumber1
andnumber2
and store the result in the variablesumResult
.Display Result: The
fprintf
function is used to display the result. It prints a formatted message showing the numbers and their sum.Run the Code: Save the script with a
.m
extension (e.g.,sum_example.m
) and run it in MATLAB.Result:
The sum of 5 and 7 is: 12
0 Comments