I need to create a program that will add consecutive odd numbers until they reach a user defined value. I then need the last number added to be displayed.
Show older comments
I need to use a for loop to solve this. I am a little confused how to approach this problem and would appreciate any guidance.
You first specify a number that you want to reach, example: 1,000 You are adding odd whole numbers together, in order: 1 + 3 + 5... to reach this value. What will be the last number added that will make the sum greater than a user defined value (1,000) ?
Answers (1)
Geoff Hayes
on 12 Oct 2015
0 votes
Mallory - see for loop for example of how to use a for loop. Pay particular attention to the description for the step parameter of the loop. You will want to use this as your step size will be two (since you are concerned with odd numbers only). Try putting together some code that will execute this loop (which starts at one and steps by two) and sums up each index (again refer to the description for this) until 1000 is exceeded. You will need an if statement to check this condition.
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!