Ok, whenever i input an even number' it keeps adding and additional 2. ex, if i input 22 it should list 2, 4, 6 .... 22. but instead it stops at 24. Why is this?
cout << "Please input a number greater then zero to find the multiples of the even\nand odd numbers. If you would like to exit please input zero";
cin >> number;
que1 = number % 5;
que2 = number % 2;
que3 = number % 1;
{
if (que1 == 0)
for (display = 0; number > display;)
{display = display + 5; //I had to do it this way because I could not put it in the for statement
cout << display << " " ;}
Its an assignment, ik it looks weird. We are almost forced to use a FOR, WHILE, AND A DO. everything is working in my program besides a random 2 being added when an even number is inputed.
NOTE: In simple terms the program is suppose to receive a number, and out multiples of it. 22 = 2, 4, 6, 8...... and 25 would be 5, 10 ,15..... 9 being 1, 3, 5..... and so forth. while making sure the user does not input a negative number and allows them to exit by inputin 0
ou are to write a program to utilize three different looping structures: WHILE,
FOR, and DO. Your program should loop processing input from the user until he indicates
that he wants to quit by entering a zero for the integer.
INPUT: Prompt the user to enter a positive integer or a zero to quit. You may
assume that the user has entered an integer but you must make sure that it is
positive or zero. If it is not positive or zero, loop until the user enters a positive
integer or zero. If the user enters a zero, the program should end.
PROCESSING: If the number entered is a multiple of 5, print all the multiples of 5
from 5 through the number entered. Use a FOR loop to generate the numbers.
If the number entered is an even number, print all even numbers from 2 through the
number entered. Use a WHILE loop to generate the numbers.
If the number entered is an odd number, print all odd numbers from 1 through the
number entered. Use a DO loop to generate the numbers.
Only print one set of numbers for each value entered. For example, if 20 is entered,
print 5 10 15 20 but do not print 2 4 6 8 ... 20.
OUTPUT: Print the desired numbers to the screen.
METHODOLOGY: If the remainder of the number divided by 5 is 0, the number is a
multiple of 5. If the remainder of the number divided by 2 is 0, the number is even.
#include <iostream>
int main()
{
int number = 1 ;
while( std::cout << "a number greater then zero (0 or negative to quit)? " &&
std::cin >> number && number > 0 )
{
// If the number entered is a multiple of 5, print all the multiples of 5
// from 5 through the number entered. Use a FOR loop to generate the numbers.
if( number%5 == 0 ) for( int n = 5 ; n <= number ; n += 5 ) std::cout << n << ' ' ;
// If the number entered is an even number, print all even numbers from 2 through the
// number entered. Use a WHILE loop to generate the numbers.
elseif( number%2 == 0 )
{
int n = 2 ;
while( n <= number ) { std::cout << n << ' ' ; n += 2 ; }
}
// If the number entered is an odd number, print all odd numbers from 1 through the
// number entered. Use a DO loop to generate the numbers.
else
{
int n = 1 ;
do { std::cout << n << ' ' ; n += 2 ; } while( n <= number ) ;
}
std::cout << '\n' ;
}
}