Sum of N Integers - Even Reversed
The program must accept N integers and print the sum S of all POSITIVE integers with the even positive integers reversed.
Input Format:
The first line will contain N.
The second line will contain N integers separated by a space.
Output Format:
The first line will contain S.
Boundary Conditions: 1 <= N <= 100000
Example Input/Output 1:
Input:
4 39 -8 57 24
Output:
138
Explanation:
The sum = 39+57+42 = 138 (The even number 24 is reversed)
Example Input/Output 2:
Input:
3 -23 -11 -445
Output: 0
Code:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n,i,temp=0,sum=0;
cin>>n;
int arr[n];
for(i=0;i<n;i++)
{
cin>>arr[i];
if(arr[i]>0)
{
if((arr[i]%2==0))
while(arr[i]>0)
{ temp*=10;
temp+=arr[i]%10;
arr[i]=arr[i]/10;
}
sum+=arr[i]+temp;
temp=0;
}
}
cout<<sum;
}
No comments:
Post a Comment