Write a program that initializes the values in one-dimensional array and computes the sum of even values assigned to the array. It also displays even values and their sum on the screen.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and variable i, sum.
- Step 3: Initialized array and sum = 0;
- Step 4: Divide array element one by one with 2, if reminder is zero then add value in sum.
- Step 5: Repeat step 4 until i<=9.
- Step 6: Print sum
- Step 7: End program
Code:
Write a program that initializes the values in a one-dimensional array and computes the sum of even values assigned to the array. It also displays even values and their sum on the screen.
#include"iostream"
using namespace std;
main()
{
int arr[10]={14,16,34,43,77,34,21,12,33,30};
int i, sum;
i = sum = 0;
while(i<=9)
{
if(arr[i]%2==0)
{
cout<<arr[i]<<endl;
sum = sum + arr[i];
}
i++;
}
cout<<"\nSum of Even Values : "<<sum;
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well๐ https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: ๐https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: ๐https://myustaadg.com/category/programming-exercises/arrays-exercises/