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...
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...
C++ PROGRAMMING Assignment: For this assignment, you will construct a program which is capable of taking...
C++ PROGRAMMING Assignment: For this assignment, you will construct a program which is capable of taking a user-given number, and adding up all of the numbers between 1 and the given number. So if someone inputs 12, it should add 1 + 2 + 3 + 4 + … 9 + 10 + 11 + 12, and return the answer. However, you’ll be using two different methods to do this. The first method should utilize either a for loop or...
If you roll a die, you get one of the following numbers: 1, 2, 3, 4,...
If you roll a die, you get one of the following numbers: 1, 2, 3, 4, 5, 6. Each possibility occurs with equal probability of 1/6. The expected value of a dice roll is E(D)= 3.5 and the variance of a dice roll is Var(X) = 2.917. a) Suppose you roll a die and then add 1 to the roll to get a new random variable taking one of the following numbers: 2,3,4,5,6,7. What is the variance of this new...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT