i m doin this code......here shows all the even numbers from random nubers bt i want to find first 3 even nuber....any solution?
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void sum_even(int , int ,int );
void sum_even(int a[] , int size ,int sum ){
sum=0;
for(int i=0;i<size;i++){
if(a[i]%2==0){
cout<<"Even Found position at "<<i<<" Number is "<<a[i]<<endl;
sum=sum+a[i];
}
}
cout<<endl<<"Sum "<<sum;
}
int main(){
int size ,sum=0 ;
cout<<"Enter Size ";
cin>>size;
int a[size];
srand(time(0));
cout<<"Randomly number generate "<<endl;
for(int i=0;i<size;i++){
a[i]=rand()%50;
}
If I am not wrong, this is what you want to do:
Getting the sum of first 3 random numbers:
So, you have to modify your function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void sum_even(int a[] , int size ,int sum ){
sum=0;
int a1=0;
for(int i=0;i<size;i++){
if(a[i]%2==0&&a<3){
cout<<"Even Found position at "<<i<<" Number is "<<a[i]<<endl;
sum=sum+a[i];
a1++;
}
if(a1==3){
break;
}
}
cout<<endl<<"Sum "<<sum;
}