0

This is an assignment that I'm having trouble with, these are the 2 questions I'm having trouble with.

2. Write a loop that counts only the odd numbers out of the first five numbers entered from the keyboard, but does not count (skips) the number 7. Use the keyword continue to skip 7 inside the loop
3. Write the same program above, but exit the loop using break if the number 7 is entered

So my program works just fine but I'm not sure how to implement the two 7 parts?? Any help would be appreciated.

#include <iostream>
using namespace std;


int main(){

int nNums = 5;
int count=0,i;
int number;

for (i=0;i<nNums;i++){

cout << "Please Enter a Number: ";
cin >> number;

if (number%2 != 0) count++;
}



cout << "Number of odd numbers: " << count << endl;
return 0;
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by mike_2000_17
1
if(number == 7) continue;

This will skip the rest of the loop if the number is 7, and it will go back to the start of the loop (i.e. it will increment i, and check the end-condition).

if(number == 7) break;

This will terminate the loop if the number is 7, and it will resume execution after the closing } of the loop.

Votes + Comments
Thanks
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.