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.
Answers
Answer:
C++.
Explanation:
#include<iostream>
using namespace std;
int main()
{
int n,sum1=0,sum2=0;
int arr[n];
cin>>n;
int i;
for(i=1;i<=n;i++)
{
cin>>arr[i];
}
for(i=1;i<=n;i++)
{
if(arr[i]%2!=0)
{
sum1=sum1+arr[i];
}
else
{
sum2=sum2+arr[i];
}
}
cout<<sum2<<"\n"; \\sum of even numbers
cout<<sum1; \\sum of odd numbers
}
#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;
}
All Cases Passed