answersLogoWhite

0

Algorithm: sum_even

Input: an integer, n, such that n>0

Output: the sum of all even integers within the open range (1:n)

sum := 0

val := 2

while (val < n) do

{

sum := sum + val

val := val + 2

}

return sum

Note that you explicitly specified between 1 and n, which literally means both 1 and n should be excluded from the sum. 1 would be excluded anyway since it is not an even number, however if we wish to include n, then use the following algorithm instead:

Algorithm: sum_even

Input: an integer, n, such that n>0

Output: the sum of all even integers within the half-open range (1:n]

sum := 0

val := 2

while (val <= n) do

{

sum := sum + val

val := val + 2

}

return sum

User Avatar

Wiki User

8y ago

What else can I help you with?

Related Questions

Can you write an algorithm to find the beast numbers?

maybe


Write algorithm and draw flowchart to find the sum of even numbers?

jgfujtf


Write an algorithm to find the root of quadratic equation?

Write an algorithm to find the root of quadratic equation


How. to. write an. algorthim. to find the. sum of. first. 15 natural. numbers?

Write an. Algorthim. To. Find the. Sum. Of. First15 natural. Numbers


How can you use the Euclidean Algorithm to find the GCF of three numbers?

By dividing


What are the examples of algorithm in the flow chart?

TO find the sum of n numbers?


Write a algorithm to find sum of n integers?

Yes, please do.


What is the algorithm of LCM of more than 3 numbers?

If you use methods based on prime factors, it is the same whether you have 2, 3, or more numbers: find all the factors that occur in any of your numbers. If you use a method based on Euclid's Algorithm (that is, lcm(a, b) = a x b / gcf(a, b), where you find the gcf with Euclid's Algorithm), then you can find the lcm for two numbers at a time. For example, to get the lcm of four numbers, find the lcm of the first two, then the lcm of the result and the third number, than the lcm of the result and the fourth number.


Can you write an algorithm to find the greatest of three number using class?

Yes. But why?


An algorithm to find whether a directed graph is connected or not?

You can use a The Depth-First Search algorithm.


Write algorithm and draw a flow chart to find the total and average of a student for six subjects?

write an algorithm to compute the weekly average rainfall given the daily rainfall for four weeks


Is there an algorithm that will automatically solve a Rubik's cube?

Such an algorithm is called a 'God algorithm', from the fact that it would only be known by god/the gods. Although many have tried to find it, none have so far discovered it (assuming it exists). To solve a cube by algorithm, you need to know the appropriate algorithm to apply and when in the stages of solving; different sets can use large numbers of algorithm.