Find out the summations of even and odd numbers




Algorithm 2.3:
Algorithm to find the summation of even and odd numbers

Input: A[1...n], sum_odd = 0, sum_even = 0;
//An array and variables to store the summation
2. for (i = 1; i ≤ n; i = i + 1)
{
if (A[i]%2 = = 0), sum_even = sum_even + A[i];
else sum_odd = sum_odd + A[i];
}
3. Output: Summation of odd numbers (print sum_odd) and summation of even numbers (print sum_even)


Find Out The Summations Of Numbers In Odd Index And Even Index Separately

Problem 2.4:
Given a list of integers stored in a linear array, find out the summations of numbers in odd index and even index separately.

Solution:
This problem is similar to the problem 2.3. Here the difference is that, we have to check whether the index is odd or even.

Summation Of Even And Odd Indexed Numbers
Algorithm 2.4:
Algorithm to find the summation of even and odd indexed numbers
Input: A[1...n], sum_odd = 0, sum_even = 0;
//An array and variables (to store the summation)
2. for (i = 1; i ≤ n; i = i + 1)
{
if (i%2 = = 0), sum_even = sum_even + A[i];
else sum_odd = sum_odd + A[i];
}
3. Output: Summation of numbers in odd indices (print sum_odd) and summation of numbers in even indices (print sum_even)




preview <<





No comments:

Post a Comment

There was an error in this gadget

Followers

About Me

Dhaka, Dhaka, Bangladesh
..