2. Against All "Odds"
by CodeChum Admin
Looping numbers is fun, but it's even more exciting when we combine complex tasks to it, just like looping through a series of numbers and performing a series of code only to special numbers, like odd ones! And today, we're going to do just that.
Are you ready?
Instructions:
Input a random positive integer. This will serve as the starting point of the loop.
Using a while() loop, print out all the odd numbers starting from the inputted integer, until 0. The outputted numbers must all be separated by line.
Input
A line containing an integer.
10
Output
Multiple lines containing an integer.
9
7
5
3
#include<iostream>
using namespace std;
int main()
{
int value;
do
{
cout<<"Please, input a random positive integer (0 - exit program): ";
cin>>value;
if(value<=0)break;
int i=value;
while(i>=0)
{
if(i%2==1)
{
cout<<i<<endl;
}
i--;
}
}while(true);
}
Comments
Leave a comment