Using repeating loops to calculate profits

2 views (last 30 days)
Write a program which calculate the total profit/year by the end of each month in a year, given the variable P which stores profit/month for that year:
P= [10 7 5 4 6 8 1 6 8 5 11 12]*1000
Set up a loop to calculate Ptotal(i+1)=Ptotal(i)+P(i+1), to help decide about start/end conditions, use an iteration table.
clear
clc
P= [10 7 5 4 6 8 1 6 8 5 11 12]*1000;
Ptot = 1;
for i = 1:length(P)
if Ptot(i)==1
Ptot=P
else
Ptot(i+1) = Ptot(i)+P(i+1)
end
end
disp(Sum);
Could someone help me with this, i've been stuck on this question for a while and just kind of lost? I did a similar question earlier and got the right answer but because the index is start at 0 instead of 1 is making me kind of confused on how to go about this question?
Thanks for the help!

Answers (1)

Ashutosh Thakur
Ashutosh Thakur on 30 Jun 2022
You can follow this code for understanding about start/end conditions
P= [10 7 5 4 6 8 1 6 8 5 11 12]*1000;
Ptot(1) = P(1);
for i = 1:length(P)-1
Ptot(i+1) = Ptot(i) + P(i+1);
end
disp(Ptot)
10000 17000 22000 26000 32000 40000 41000 47000 55000 60000 71000 83000
You can initialize Ptot(1) with P(1) and run the loop till length(P) - 1 because you are incrementing Ptot by i+1.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!