Write a code segment using a for loop that outputs numbers f

Write a code segment using a \'for\' loop that outputs numbers from -10 to 10. write a code segment using a \'for\' loop that sums the integers, counting up from 1, and stops when the sum is greater than 10,000, printing out the integer that was most recently added to the sum.

Solution

for i=-10:10%taking i from -10 to 10 with step size 1
disp(i);%displays i values
end
sum1 = 0;
for i=1:Inf%takes i from 1 to Inf
sum1 = sum1+i;%adds i to the sum
if sum1>10000%whenever sum1 is greater than 10000
break;%the loop stops
end
end
disp(i);%displays the i at which loop was stopped

Command window output:

>> clear all
-10

-9

-8

-7

-6

-5

-4

-3

-2

-1

0

1

2

3

4

5

6

7

8

9

10

Warning: FOR loop index is too large. Truncating to 9223372036854775807. %this causes because of Inf
> In simpletask1 at 5
141%the answer is 141

>>

 Write a code segment using a \'for\' loop that outputs numbers from -10 to 10. write a code segment using a \'for\' loop that sums the integers, counting up fr
 Write a code segment using a \'for\' loop that outputs numbers from -10 to 10. write a code segment using a \'for\' loop that sums the integers, counting up fr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site