This is the description of the prac im doing. The code below is what I have come up so far. I also attached the pseudocode as well. And for me it's confusing somehow. However, I'm wanting to ask that how to use a while loop to force odd even number.
Write a program that prints all the odd or even integers between any two integers (inclusive) entered by the user. The user should be able to select whether odd or even integers are to be printed.
Use a while loop to force correct entry for the odd/even choice (use type char for choice).
The program should determine which is the smaller of the two integers entered. If the second integer is smaller, then swap – but do NOT use the swap() function. Pass the numbers and your choice of odd or even to a function.
Your code should contain the odd/even conditional statements before a while loop, and use a loop increment of two, looping from the smaller number towards the larger number. This code should be in a separate function, not in main().
PSEUDOCODE
Prompt and read first integer
Prompt and read second integer
If second integer is smaller
Swap (do NOT use the swap() function)
End if
Prompt and read choice of even or odd
Invoke foo() to print
void foo()
If (num1 is odd and choice is even) or (num1 is even and choice is odd)
Add 1 to num1
End if
Print all even or odd (as per choice) from num1 to num2 (inclusive)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
int main()
{
int number;
int remainder = 1;
char choice;
cout << "Please enter the first integer: ";
cin >> number;
cout << "Please enter the second integer: ";
cin >> number;
while ( choice )
{
if ( number % 2 == 0)
{
cout << "\t\t " << number << " is even.\n";
}
else
{
cout << "\t\t " << number << " is odd.\n";
}
}
getch();
}
|