c++ code for odd and even numbers code example
Example 1: return odd numbers c++ for loop
#include<iostream>
int copyOdd (int A[], int sizeA, int B[]);
int main ()
{
int arr [] {1,2,3,4,5,6,7,8,9,10};
int Odd [5] {0,0,0,0,0};
copyOdd(arr,9,Odd);
for(int i = 0 ; i < 5; ++i)
{
std::cout<<Odd[i]<<' ';
}
std::cout<<std::endl;
return 0 ;
}
int copyOdd (int A[], int sizeA, int B[])
{
int j {0};
B [5];
for(int i = 0 ; i < sizeA ; ++i)
{
if(A[i]%2 == 1)
B[j++] = A[i];
}
return B[5];
}
Example 2: c++ sum of even and odd numbers
void sumEvenAndOdd(){
int n=0 , evenSum=0 , oddSum=0;
int numbers[Size];
cout<<"enters the number(N) of integers\n";
cin>>Size;
cout<<"Enter "<<Size<<" number/s:\n";
for(int i =0 ;i< Size ; ++i){
cin>>numbers[i];
}
for(int i =0 ;i<Size ; ++i){
if(numbers[i]%2 ==0){
evenSum += numbers[i];
}
else{
oddSum += numbers[i];
}
}
cout<<"the sum of even numbers is : "<<evenSum <<"\nthe sum of odd numbers is :"<<oddSum<<endl;
}