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??
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.
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 ++)
#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;
}
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.
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.
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
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.
#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