1.input a positive integer.This will serve as the starting point of the loop.
2.Using a while()loop, print out all the odd numbers starting from the inputted integer,until 0.The outputted numbers must be separated by line.
3.Also remember that since the loop goes to descending order,a decrement variable shall be created and decreased per iteration for the loop to terminate when it reaches 0
#include <iostream>
using namespace std;
int main() {
int Item;
cout<<"Please input a positive number: ";
cin >> Item;
while (cin.fail()||(Item<=0))
{
cin.clear();
cin.ignore(3278,'\n');
cout<<"error please input positive number:\n";
cin >> Item;
}
int i=Item;
while(i!=0)
{
if(i%2==1)//if cur number is odd number
cout<<i<<endl;
--i;
}
}
Comments
Leave a comment