+ 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

20th Feb 2024, 2:55 PM
Solo Ams
Solo Ams - avatar
5 ответов
+ 3
Use for loop and check if number is even (i % 2 == 0) and then add in a variable called sum.
20th Feb 2024, 4:23 PM
A͢J
A͢J - avatar
+ 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; }
20th Feb 2024, 5:08 PM
Solo Ams
Solo Ams - avatar
0
Hello NB‎ Find my code above pls A͢J I actually did use the for loop
20th Feb 2024, 5:17 PM
Solo Ams
Solo Ams - avatar
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
20th Feb 2024, 5:26 PM
Solo Ams
Solo Ams - avatar
0
Write a program to find area and circumference of circle
22nd Feb 2024, 2:51 PM
tej bahadur singjali
tej bahadur singjali - avatar