Question: Ask the user for a number between 5 and 30. For each number between 1
ID: 3850291 • Letter: Q
Question
Question:
Ask the user for a number between 5 and 30. For each number between 1 and the number entered, display the value 1/that number, as a ratio and a decimal value. Round up on .005 and above. For example:
Use the following format specifiers to get the precision of the output to match:
This is my code so far.
#include <iostream>
#include<iomanip> //for formatting
using namespace std;
int main(){
int n;
//ask user to enter a number
cout<<"Please enter a number (5 to 30): ";
cin>>n;
cout << n << endl;
cout << fixed << setprecision(2); //to print upto 2 decimal places
//check for range
if(n>=5 || n<=30){
//if out of range
cout<<"Please follow the directions! ";}
else{
//if in range loo from 1 to that number
for(int i=1; i<=n;i++){
cout<<"1/"<<i <<"is"<<1.0/i<<endl;}}
return 0;}
Here are expected results.
1. Compare output Input 5 Please enter a number (5 to 30) 5 Your output Please follow the directions! Please enter a number (5 to 30) 5 1/1 is 1.00 1/2 is 0.50 J Expected output 1/3 is 0.33 1/4 is 0.25 1/5 is 0.20 2. Compare output Input 3 Please enter a number (5 to 30) 4 Your output Please follow the directions 3. Compare output Input 31 Please enter a number (5 to 30) 31 Your output Please follow the directionsExplanation / Answer
I have updated the code below,you have mentioned your OR condition wrongly I have highlighted that condition.
Answer:
#include <iostream>
#include<iomanip> //for formatting
using namespace std;
int main(){
int n;
//ask user to enter a number
cout<<"Please enter a number (5 to 30): ";
cin>>n;
cout << n << endl;
cout << fixed << setprecision(2); //to print upto 2 decimal places
//check for range
if(n < 5 || n > 30){
//if out of range
cout<<"Please follow the directions! ";}
else{
//if in range loo from 1 to that number
for(int i=1; i<=n;i++){
cout<<"1/"<<i <<"is "<<1.0/i<<endl;}}
return 0;}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.