I dont need the whole program.. just the loop part of it. Best answer gets the points (not neccissarily the first..)
Write a while loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integer that is not positive. The values should be separated by single blank spaces. Declare any variables that are needed.
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
int number=0;
while(number > -1)
{
cout<<"Enter a positive number: ";
cin>> number;
if(number >=100)
{
cout<<number<< "is equal to or greater than 100"<<endl;
}
}
int N;
while(1)
{
cin>>N;
if(N<0) break;
if(N>100) cout<<N<<" ";
}
While running you can go on enter a set of integers followed by a negative number at the end like
10 109 192 22 22 910 22 012 -89
Output will be
109 192 910
When you say "numbers", i'm guessing you need more than 1 number. since we don't know how many numbers you need, you might want to use containers to add those numbers in.
vector<int> numbers;
int n;
//ask for input
cin >> n;
while(n > 0) {
//add the number to the vector
numbers.push_back(n);
//ask for input again
cin >> n;
}
//printing the numbers
for(size_t i = 0; i != numbers.size(); ++i) {
if(numbers[i] >= 100)
cout << numbers[i] << ' ';
}
you could use iterators if you want.