Answer to Question #44308 in C++ for hamza

Question #44308
Write a C-program that asks the user to type an integer N for finding the sum of the
numbers 2, 4, 6, 8, …, n
1
Expert's answer
2014-07-22T10:03:22-0400
#include <iostream>
int calculateSum(int *arr, int size)
{
int sum = 0;
for(int i = 0; i < size; ++i)
sum += arr[i];
return sum;
}
int main()
{
int size;
std::cout << "Enter size: " << std::endl;
std::cin >> size;
size /= 2;
int *arr = new int[size];
for(int i = 0; i < size; ++i)
{
arr[i] = (i + 1) * 2;
}
std::cout << "Sum: " << calculateSum(arr, size) << std::endl;
std::cin.get();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS