I am suppose to write a program that uses one variable int n and asks the user to input an even number. If the user types in a even #, the program prints the #. if the users enters an odd #, the user is asked to enter the even # again. Then the code prints whether the # is even or odd.
The modulus function (%) comes in handy in this case. Remember- the modulus function divides and outputs the remainder. What number evenly divides into all even numbers, but no odd numbers?
if ( n % 2 == 0 )
cout << n << "is even";
else if ( n % 2 !=0 )
cout << n << "is odd";
Not sure what to do for "If the user types in a even #, the program prints the #. if the users enters an odd #, the user is asked to enter the even # again."
//EnterEven.cpp
//This program need an even number. -mandatory
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
bool keepLooping=true;
while(keepLooping){ //while keepLooping is true
cout<<"Enter a number: ";
int number;
cin>>number;
if(number%2==0)
keepLooping=false;
}//end while
return 0; //indicates success
}//end main
Enter a number: 1
Enter a number: 1
Enter a number: 3
Enter a number: 5
Enter a number: 4
int n;
while (keepLooping) {
cout << " Input an even number.";
cin >> n;
if ( n % 2 ==0 )
cout << n << " " << endl;
if ( n % 2 ==0 )
keepLooping=false;
if ( n % 2 ==0 )
cout << n << " is even." << endl;
}