Loop

Exercise 1. Write a program that uses one variable int n and asks the user to input an even number. If the user enters an even number, the program prints the value entered by the user. If the user enters an odd number, the user is asked again to enter an even number. Then the code should finish by printing the number and whether the number is even or odd.



How do i do this? we only just learned loops such as using i=1, i++, i <= n




Post your attempt so we can tell you what you're doing wrong
for loops are used when the number of times a block needs to be executed is known.

Is this your case? That is, do you know the number of times a user will enter an odd number?

If not, I would recommend using a while loop instead.
#include <iostream>
using namespace std;

int main() {

int n;
cout << "Enter the even number";
cin >> n;
int i=1;
while ( i <= n )
if ( n % i != 0 )
cout << " Enter the even number. ";
else if ( n % i == 0 )
cout << n << " is even."<<endl;
i++;
return 0;
not sure what im doing...:(

confused on how to do " If the user enters an odd number, the user is asked again to enter an even number. "
Last edited on
closed account (o3hC5Di1)
Hi there,

This part of your code does exactly that:

1
2
3
4
if ( n % i != 0 )
cout << " Enter the even number. ";
else if ( n % i == 0 )
cout << n << " is even."<<endl;


operator%() is called the modulus operator. It returns the remainder of the division of its left operand by its right operand, for instance:

3 % 2 = 1 (1x2 =2 +1=3)
12 % 5 = 2 (2x5 = 10+2=12)

Therefore, if a number can be divided by 2 without any remainder, it is even. If it does have any remainder, it's an odd number.

Hope that makes sense.

All the best,
NwN
ok i changed it
#include <iostream>
using namespace std;

int main() {

int n;
cout << "Enter the even number";
cin >> n;
if ( n % 2 != 0 )
cout << " Enter the even number. ";
else if ( n % 2 == 0 )
cout << n << " is even."<<endl;
return 0;
}
but how do i keep making it ask the user when the user types in an odd number tho?
Last edited on
closed account (o3hC5Di1)
Hi there,

Please wrap your code in [code][/code]-tags, it makes it more readable.

You will have to use a loop, loops keep repeating the code within them until a condition is met, whcih could be a condition you would use in an if statement (while-loop) or a counter reaching a certain value.

In this case, you could use a while loop as such:

1
2
3
4
while ( (n%2) > 0) //while n  is odd
{
    //ask for the number
}


All the best,
NwN
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int n;
cout << "Enter the even number";
cin >> n;
while( n % 2 > 0 ) 
cout << " Enter the even number. ";
if ( n % 2 == 0 )
cout << n << " is even."<<endl;
return 0;
}

when i type in 1 it makes a infinite loop. im sorry but im so confused right now
Last edited on
closed account (o3hC5Di1)
Hi there,

You are using the loop incorrectly:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int n=1;

while( n % 2 > 0 ) 
{
    cout << "Enter the even number";
    cin >> n;
}
cout << n << " is even."<<endl;
return 0;
}


The while loop will make sure the user is asked for a number until he enters an even number.

All the best,
NwN
ty so much
Topic archived. No new replies allowed.