Sum of Numbers 2-n: Repeat and While Loops

  • Thread starter Matt512
  • Start date
In summary: Yes , it is similar , i just have to replace the n with 100.3. I was thinking of using count to track the numbers , but i am not sure how to implement the "read 100 numbers" part of the question. In summary, the first algorithm can be rewritten using a repeat until loop or a while loop, but some modifications are needed to make it work correctly. The second algorithm involves finding the sum of a specific set of numbers, while the third algorithm is about reading 100 numbers and displaying the largest one.
  • #1
Matt512
17
0

Homework Statement


1.Rewrite this algorithm using a repeat until loop and then with a while loop.[/B]
Start
sum=0
Display "Input value n"
Input n
FOR( i=1 , i <=n , i+1 )
Input a value
sum=sum+value
ENDFOR
Output sum
Stop

I have some doubts if i have done the pseudo codes correctly.
2.Design an algorithm for finding the sum of the numbers 2,4,6,8,...,n
Can someone give me a leading clue on how to do it.
3.Write an algorithm to read 100 numbers then display the largest.
I am stuck midway.

Homework Equations


FOR( starting state , stopping condition, increment)

The Attempt at a Solution



Repeat until loop [/B]
Start
Repeat
sum=0
i=1
Display "Input value n"
Input n
Display "Input a value"
Input a value
sum=sum+value
i=i+1
UNTIL n>=i
Output sum
Stop

while loop.
Start
WHILE(n <>i)
sum=0
i=1
Display "Input value n"
Input n
Display "Input a value"
Input a value
sum=sum+value
i=i+1
ENDWHILE
Output sum
Stop

3.
START
count=0
sum=0
Input number
For( c=1 , c>100 , c+1)
sum=sum+value
ENDFOR
Output sum
Stop
 
Physics news on Phys.org
  • #2
Please put code (even pseudocode) inside of code tags makes it easier to read :)

Let me ask some questions:
1 (Repeat until): How does your code know where to "jump" back up to??

1 (while): When the code gets to the ENDWHILE and returns to the While <> i what gets reexecuted?
also the <> i is "NOT EQUAL TO" using it here isn't strictly wrong. But while n > i is safer. What would happen if the code accidentally jumped over n=i? 2: There are strong similarities to previous problems. This time you know already what values your going to sum up...

3: You're missing a way for the user to input a number. Also are you summing the values? or maybe you should just track something?
 
  • #3
1. ( Repeat until ) Since the until condition will still be false , it will go back up and repeats itself.
1. I think it will re execute the sum =0 , i = 1 , so i should put those two at the top and the while <>i after
 

FAQ: Sum of Numbers 2-n: Repeat and While Loops

What is the purpose of using a "repeat" or "while" loop in finding the sum of numbers 2-n?

Repeat and while loops are used in programming to repeat a certain set of instructions or code until a specific condition is met. In the case of finding the sum of numbers 2-n, these loops can be used to continuously add the numbers until the desired range is reached.

How do you initialize the sum variable in a "repeat" or "while" loop for finding the sum of numbers 2-n?

The sum variable can be initialized to 0 before the loop begins. This will ensure that the variable starts at 0 and can be updated with each iteration of the loop.

What is the difference between a "repeat" and "while" loop in finding the sum of numbers 2-n?

A "repeat" loop will execute the code within the loop a predetermined number of times, while a "while" loop will continue executing the code until a specific condition is no longer met. In the context of finding the sum of numbers 2-n, a "repeat" loop may be more appropriate if the number of iterations is known, while a "while" loop may be more suitable if the range of numbers is dynamic.

Can you use both "repeat" and "while" loops in the same program to find the sum of numbers 2-n?

Yes, it is possible to use both "repeat" and "while" loops in the same program to find the sum of numbers 2-n. However, it is important to keep in mind the purpose and conditions of each loop to ensure that they are used effectively and efficiently.

Are there any other methods besides "repeat" and "while" loops to find the sum of numbers 2-n?

Yes, there are other methods to find the sum of numbers 2-n, such as for loops and recursion. The most appropriate method may depend on the specific programming language and the complexity of the program.

Similar threads

Back
Top