Arun and Kalai were playing a puzzle game with a given set of numbers. They need to find the odd and even numbers and find the sum of the odd numbers and the sum of the even numbers. Write a program to help them to solve the puzzle game and to win the mobile phone. Input Format: Input consists of n+1 integers. The first integer corresponds to ‘n’, the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15. Output Format: Refer to sample output for details.
Answers
Answer:
#include <iostream>
using namespace std;
int main()
{
int i, num; //declare variables i, num
int oddSum=0,evenSum=0;
//declare and initialize variables oddSum,evenSum
cin>>num;
for(i=1; i<=num; i++){// for loop use to iterate 1 to num
if(i%2==0) //Check even number for sum
evenSum=evenSum+i;
else
oddSum=oddSum+i;
}
cout<<"The sum of the even numbers in the array is "<< evenSum;
cout<<"\nThe sum of the odd numbers in the array is "<<oddSum;
return 0;
}
Explanation:
Answer:
#include <iostream>
using namespace std;
int main()
{
int size;
cin>>size;
int i, num[15];
int oddSum=0,evenSum=0;
for(i=0;i<size;i++)
cin>>num[i];
//cout <<num[0]<<num[1]<<num[2]<<num[3]<<num[4];
for(i=0; i<size; i++)
{
if(num[i]%2==0)
evenSum=evenSum+num[i];
else
oddSum=oddSum+num[i];
}
cout<<"The sum of the even numbers in the array is "<< evenSum;
cout<<"\nThe sum of the odd numbers in the array is "<<oddSum;
return 0;
}
Explanation: