Question #71225

Read 10 integers from the keyboard in the range 0 - 100, and count how many of them are larger than 50, and display this result

Expert's answer

#include <iostream>
using namespace std;
int main()
{
int n,counter=0,i=1;
while(i<=10)
{
cout<<"Enter value"<<i<<" between 0 to 100 inclusive: ";
cin>>n;
if(cin.fail()||n<0||n>100)
{ cout<<"\nvalue must be 0 - 100 inclusive"<<endl;
cin.clear(); // clears the error flag
cin.ignore(24,'\n'); // Extracts characters from the input sequence and
// discards them
}
else if (n>50)
{
counter++; // count larger than 50
i++; // next input
}
else
i++; // next input
}
cout<<"\nlarger than 50 "<<counter<<endl;
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS