### Print_even

I am writing a program for school.The details are that it only prints the even numbers between 2 numbers that have been entered from the keyboard. It must include a loop and call a function: print_even.

I can get it to run but on the return value from my function, it is also returning the value... What I mean is my return statement is: "return -1;".
It returns the numbers to be printed but also the '-1'. If I put a 0 or 1, it will return that. I understand its doing what I tell it to, but I only want the numbers.

So my output is this:

Select the start number: 2
Select the end number: 10
The even numbers between 2 and 10 are 4
6
8
-1
Press any key to continue . . .

Below is my code. Any suggestions?

int print_even(int a, int b);

int main()
{

int startNumber = 0;
int endNumber = 0;

cout << "Select the start number: ";
cin >> startNumber;
cout << "Select the end number: ";
cin >> endNumber;

if (endNumber < startNumber)
{
cout << "End number is less than start number. Please try again"<< endl;
}

cout << "The even numbers between " << startNumber << " and " << endNumber << " are " << print_even(startNumber, endNumber) << endl;

system ("pause");
return 0;
}

int print_even(int a, int b)
{

for(int i=a+1;i<b;i++)
{
if(i%2!=0)
continue;
cout<<i<<endl;
}

return -1;
}
Last edited on
make datatype of ur print_even function as void. coz i dont think so that u need return; statement........... hope it will work also remove return -1;
Topic archived. No new replies allowed.