Question

MATLAB Task: Use a for loop to find how many numbers you have to add to...

MATLAB

Task: Use a for loop to find how many numbers you have to add to get a sum greater than 1,000. The numbers in the problem are consecutive, so adding up 1+2+3+4+5+6+.... until you reach the user defined value 1000.

Method : must Make a variable equal to 1 then add 2, 3, 4...to it, until you reach 1000, then you break out the loop once the sum hits 1,000.

Homework Answers

Answer #1

Following is the matlab script in which the code is implemented

MATLAB Code:

% sum is a global variable

sum = 1;

count = 1;

% for loop from 2 to 1000, we have taken upper limit as 1000 otherwise the sum exceedes 1000

for temp = 2:1000

sum = sum + temp;

count = count + 1;

% If sum exceeds 1000 then break

if (sum > 1000)

break;

end

end

% Display the output

fprintf('We need to add %d consecutive numbers to get a sum greater than 1000', count)

Proof:

1 + 2 + 3 +..................... + 44 = 990 < 1000

1 + 2 + 3 +............................ + 45 = 1035 > 1000

Please refer to the following picture for indentation and the output shown in the command window below:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop to complete the task. A while loop has this basic structure: /* variable initializations */ while (/*stop condition*/){ /* statements to be performed multiple times */ /* make sure the variable that the stop condition relies on is changed inside the loop. */ } Despite the structure of the while loop being different than that of a for loop, the concept behind it is...
JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number...
JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number equal to the sum of its divisors - 6 and 28 are the first two! 6 is 2 +3 + 1 and 28 is 14+7+4+2+1). Being the perfect number is quite unique. Only 4 perfect numbers exist between 1 and 10,000 and you’ve already found two! Please write a program to find the additional pair. Bonus: How many perfect numbers exist between 10,000 and...
Write a console application to total a set of numbers indicated and provided by a user,...
Write a console application to total a set of numbers indicated and provided by a user, using a while loop or a do…while loop according to the user’s menu choice as follows: Example Menu 1. To total numbers using a while loop 2. To total numbers using a do...while loop Enter your menu choice: 1 How many numbers do you want to add: 2 Enter a value for number 1: 4 Enter a value for number 2: 5 The total...
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT