Answer to Question #340949 in C++ for Mor

Question #340949

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

1
Expert's answer
2022-05-14T16:38:40-0400


#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;
    }


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS