+ 1
C++ issues
You want to take a list of numbers and find the sum of all of the even numbers in the list. Ignore any odd numbers. Task: Find the sum of all even integers in a list of numbers. Input Format: The first input denotes the length of the list (N). The next N lines contain the list elements as integers. Output Format: An integer that represents the sum of only the even numbers in the list. Sample Input: 9 1 2 3 4 5 6 7 8 9 Sample Output: 20
8 Answers
+ 3
Use for loop and check if number is even (i % 2 == 0) and then add in a variable called sum.
+ 1
//C++
#include<iostream>
using namespace std;
int main() {
// Program to out the sum of even numbers in a range of number
int n;
int even = 0;
cout << "Enter List size" << endl;
cin >> n;
cout << "These are all the integers in the list size" << endl;
for (int i = 1; i <= n; i++) {
cout << i << endl;
}
for (int i = 0; i <= n; i = i + 2) {
even += i;
}
cout << "The sum of the even numbers from 1 - " << n << " is: " << even << endl;
return 0;
}
0
The first for loop generates the list of values from input of the user.
The second for loop add up only the even numbers from the list generated and gives the sum
0
Write a program to find area and circumference of circle