In: Computer Science
For C++ IDE
Write an C++ to check if a number is falling within the range j...k (inclusive). Ask the user to enter both ranges j, K and the number num you want to check. Output the suitable message based on the situation Within the range Outside the range Always prompt the user to enter a value using a suitable message.
For example (multiple runs)
Enter J - beginning of the range: 3
Enter K – end of the range: 15
Enter the value you want to check: 6
6 is within the range 3 to 15
Enter J - beginning of the range: 6
Enter K – end of the range: 15
Enter the value you want to check: 6
6 is within the range 6 to 15
Enter J - beginning of the range: 6
Enter K – end of the range: 15
Enter the value you want to check: 16
16 is outside the range 6 to 15
C++ code:
#include <iostream>
using namespace std;
int main()
{
//initializing j k and num
int j,k,num;
//asking for j
cout<<"Enter J - beginning of the range: ";
//accepting it
cin>>j;
//asking for K
cout<<"Enter K – end of the range: ";
//accepting it
cin>>k;
//asking for num
cout<<"Enter the value you want to check: ";
//accepting it
cin>>num;
//checking if num is within the range
if(num>=j && num<=k)
//printing it is within the range
cout<<num<<" is within the range "<<j<<" to
"<<k<<endl;
else
//printing it is outside the range
cout<<num<<" is outside the range "<<j<<"
to "<<k<<endl;
return 0;
}
Screenshot:
Input and Output: