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


What is 323x 133 equal to in Euclid's algorithm?

To find the product of 323 and 133 using Euclid's algorithm, we first note that Euclid's algorithm is typically used to find the greatest common divisor (GCD) of two numbers rather than their product. However, the product of 323 and 133 is simply 42,919. If you were looking to apply Euclid's algorithm, you would use it to find the GCD first, which can then be used to derive other relationships between the numbers, but it does not directly provide the multiplication result.


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.


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.


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