0

Hello, new C++ user at University

I am trying to find what would be the best way to count and display the even and odd numbers in a sequence. For example, if the user inputted the sequence 2 - 6, then there would be 3 evens and 2 odds, this is the code I have

In bold is what I have now for determining this, however it doesn't always work out.

```
//prog2.cpp
//1 - 25 - 2010
//Includes the math and iostream libraries for use later
#include <iostream>
#include <cmath>
// includes the std library
using namespace std;
//Function prototype that allows us to call the function in main
bool isPrime(int num);
int main()
{
//define our low and high range variables as int
int range_low;
int range_high;
int odd_numbers;
int even_numbers;
int count_for_series;
//Starts with user input for what should be the low number in the sequence
cout << "Please input a range of numbers you would like checked, starting with the low number first :";
cin >> range_low;
//If the number entered is 0 or less, the user can fix their submission
while (range_low <= 0)
{
cout << "Error: Value entered is 0 or less, please enter a new value " << endl;
cin >> range_low;
}
//Starts the same loop but for the high range number in the sequence
cout << "Please Enter The High Number In The Range :";
cin >> range_high;
//Error checks to see if the higher number is less than the lower one, if so, allows user to change input
while (range_high < range_low)
{
cout << "The second value entered is lower value than the first, please enter a value that is lower :" << endl;
cin >> range_high;
}
//Outputs the sequence the user provided
cout << "The lowest number in the sequence is " << range_low << " and the highest number in the sequence is " << range_high << endl;
//Sees if the range_low value is prime or not, and indicates so to the user
if (isPrime(range_low) == true)
{
cout << "Starting number is prime " << endl;
}
else if (isPrime(range_low) == false)
{
cout << "Starting number is not prime " << endl;
}
//Sees if the range_high value is prime or not, and indicates so to the user
if (isPrime(range_high) == true)
{
cout << "Ending number is prime " << endl;
}
else if (isPrime(range_high) == false)
{
cout << "Ending number is not prime " << endl;
}
[B] // This is where we calculate how many even and odd numbers are in the series
count_for_series = range_high - range_low;
if (count_for_series % 2 == 0)
{
even_numbers = count_for_series / 2;
odd_numbers = even_numbers - 1;
}
else if (count_for_series % 2 == 1)
{
odd_numbers = count_for_series / 2;
even_numbers = odd_numbers - 1;[/B]
}
//Outputs the number of odd numbers followed by the even
cout << "The number of odd numbers in the sequence is " << odd_numbers << " and the number of even numbers is " << even_numbers << endl;
return(0);
}
//Function for determining if a number is prime or not
bool isPrime(int num)
{
int i;
if (num < 0)
{
num = -num;
}
if (num == 0)
{
return(false);
}
else if (num % 2 == 0)
{
return(false);
}
for (i = 3; i <= sqrt(num); i += 2)
{
if (num % i == 0)
{
return(false);
}
}
return(true);
}
```