use a for loop to add all numbers from 1 to 20 and print the sum of the numbers in matlab

Here's an example code using a for loop to add all numbers from 1 to 20 and print the sum of the numbers in Matlab:

main.m
sum = 0;
for i = 1:20
    sum = sum + i;
end
disp(sum);
56 chars
6 lines

Output:

main.m
210
4 chars
2 lines

In this code, we first initialize the sum variable to 0. Then we use a for loop to iterate from 1 to 20 and add each number to sum. Finally, we print the sum using the disp command.

related categories

gistlibby LogSnag