I need to write a for loop that will find the largest value.

I need function to allow the user to enter a value for k and a value for limit from the keyboard. The function is to then find the largest value of k such that; 1^1/10 + 2^2/10 ... k^k/10 <= limit
Im not really sure where to start other than having the variable ready for user input. even just a little bit of a prompt would help greatly!

Answers (1)

k = 10 ;
limit = 100;
thesum = 0 ;
for i = 1:k
thesum = thesum+i^i/10 ;
if thesum >= limit
break
end
end

1 Comment

This is what I wrote before I saw your answer. It doesnt run how I was expected, can you see any major issues?
limit = input('Please specify your limit: ');
k = input('Provide a k value you wish to use: ');
for n = 1:k
x = sum(n.^(n/10))
if (x > limit)
r = 1:limit;
n = 1:k-r;
x = sum(n.^(n/10));
k=n(end)
else
disp('The largest value for k is:')
disp(k)
break
end

Sign in to comment.

Categories

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

Asked:

on 27 Oct 2021

Commented:

on 27 Oct 2021

Community Treasure Hunt

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

Start Hunting!