Please Help

Pages: 12
I need to write a program that prints all the even numbers between any two numbers using print_even that takes the initial and ending number as parameters, and prints all the intervening even numbers. I have to use a loop to implement this??


Any ideas?!?
Take in your first parameter as your starting point,
if(firstNum % 2 == 0) // The number is even

then print out that number and start incrementing by two.

if(firstNum % 2 != 0) // The number is odd

then increment by one, print the new number and start incrementing by two
Not understanding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void print_even(int firstNum, int lastNum)
{
    int currentNum = firstNum;
    if(firstNum %2 == 0)    // The number is even
    {
        currentNum += 2;    // Increment by two since it is even
    }
    else                    // The number is odd
    {
        currentNum += 1;   // Increment by one since it is odd
    }

    // Print all even numbers until lastNum is reached
    while(currentNum < lastNum)
    {
        std::cout << currentNum << std::endl;
        currentNum += 2;
    }   
}



The print_even function would look something like this, firstNum % 2 checks the remainder after dividing firstNum by 2, so if firstNum is even then the remainder will be 0, otherwise it is an odd number if the remainder is not equal to 0.

So once you have the next even number after firstNum, you start incrementing by 2 and printing out the number each time until you reach the lastNum.
Last edited on
A simpler, albeit slower, method is to just use a simple for loop:
1
2
3
4
5
void PrintEvenNumbers(int start, int end) {
   for (int i = start, i <= end; i ++)
      if (i % 2 == 0)
         cout << i << "\n";
}


The code above will print out the starting and ending numbers if they're even as well however. If you don't want to print them out, in case they're even, just change the for line to:
for (int i = start + 1; i < end; i ++)

Simple function.
Last edited on
1
2
3
4
5
6
7
8
if(num == odd)
   num++;

while(num < end)
   {
    output num;
    num = num + 2
   }
None of these codes compile when I try it??
What errors are you getting? You can't just copy that code directly and run it. You will need the main function and to pass values in etc.
#include <iostream>
int main();
using namespace std;
void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}

// Print all even numbers until lastNum is reached
while(currentNum < lastNum)
{
std::cout << currentNum << std::endl;
currentNum += 2;
}
return 0;
}
It doesn't work??
You are defining a function inside main(). Move the function definition outside and it should work. If you put the definition after main you'll have to add a prototype before main so that when you call the function the compiler at least knows what it is.
I am sorry, I am fairly new at this. How do I define it outside of main?
Read this:

http://www.cplusplus.com/doc/tutorial/functions/

That should help speed you on your way. :) Pay special not to where the addition() function definition is in relation to main().

Also, in the 2nd page (Functions II), the part on declaring functions talks about putting function prototypes before main and definitions after main. Whether you do it this way or by simply putting the entire definition before main is up to you.
Last edited on
Ok so I have put main outside and now, nothing runs when I compile it because I need to call my function inside of main, but again how?? This is where I am not getting it

#include <iostream>

using namespace std;

void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}

// Print all even numbers until lastNum is reached

while(currentNum < lastNum)
{
std::cout << currentNum << std::endl;
currentNum += 2;
}
}
int main()
{
system ("PAUSE");
return 0;
}
Anyone able to help?
You need to actually call the function.
Take another look at the tutorial page linked above by Raezzor.
And I said move the function definition out of main() not move main() out of the everything else. :p
Am I getting somewhere?

#include <iostream>

using namespace std;

void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}
// Print all even numbers until lastNum is reached
int main ();
{
while(currentNum < lastNum)
std::cout << currentNum << std::endl;
currentNum += 2;
system ("PAUSE");
return 0;
}
}
Not really. The operating system calls main(). The code then executes until it reaches the end of main (where it says return 0 in main). Do you see any problems relating to this in your code?

Also, please use the code tags (<> button on the right when responding or editing) to post your code. This way it'll keep all the indentations you use and make the code easier to read.
Last edited on
#include <iostream>
int main;
using namespace std;

void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}
// Print all even numbers until lastNum is reached

{
while(currentNum < lastNum)
std::cout << currentNum << std::endl;
currentNum += 2;
system ("PAUSE");
return;
}
}
Pages: 12