answersLogoWhite

0


Best Answer

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

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

ans this question

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write an algorithm find out the sum of first n odd numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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


Write an algorithm and draw flowchart to calculate the perimeter of a square?

write an algorithm and draw a flow chart to find perimeter of a square


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?


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.


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 a algorithm and flowchart to find largest among N numbers?

Max = 0For K = 1 to NIf Number(K) > Max then Max = Number(K)Next KPrint Max