In: Computer Science
Input 10 integers and display the following:
a. the sum of even numbers.
b. the sum of odd numbers.
c. the largest integer
d. the smallest integer
e. the total count of even numbers
f. the total count of odd numbers.
Using C++ program with for loop..
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[10];
int i,oddSum=0,evenSum=0,countodd=0,counteven=0,max,min;
cout<<"enter the values";//enter 10 integers
for (i = 0; i < 10; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < 10; i++)//find largest number
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < 10; i++)//find smallest number
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max;
cout << "Smallest element : " << min;
for(i=0; i<10; i++)//sum and count of even and odd
numbers
{
if(arr[i]%2==0)
{
evenSum=evenSum+arr[i];
counteven++;
}
else
{
oddSum=oddSum+arr[i];
countodd++;
}
}
cout<<"The sum of odd numbers are:"<<oddSum;
cout<<"\nThe sum of even numbers are:"<<evenSum;
cout<<"\nThe count of odd numbers
are:"<<countodd;
cout<<"\nThe count of even numbers
are:"<<counteven;
getch();
return 0;
}