Q2: Create a C++ program that does the following
a. Declare an array of integers with a size of 10 and initialize it with some values
b. Find and display the sum of all even numbers in the array.
c. Find and display the product of all odd numbers in the array.
d. Determine and display the average of all numbers in the array.
#include <iostream>
using namespace std;
int main()
{
int array[]={1,2,3,4,5,6,7,8,9,10},seven=0,sodd=1,saverage=0;
for (int i=0;i<10;i++)
{
if (array[i]%2==0)
{
seven+=array[i];
}
else
{
sodd*=array[i];
}
saverage+=array[i];
}
float average;
average=saverage/10;
cout<<"Sum of even numbers in array is "<<seven<<endl;
cout<<"Product of odd numbers in array is "<<sodd<<endl;
cout<<"The average of these numbers is "<<average<<endl;
}
Result:
Comments
Post a Comment