I\'m bad with for loops can I please get some writing this program 1:Prompt and
ID: 672667 • Letter: I
Question
I'm bad with for loops can I please get some writing this program
1:Prompt and read in an integer value for the low end of the range. This value
must be greater than one. Print the message "Number must be greater than 1."
if an invalid value is input. Repeat this until a valid value is input.
2: Prompt and read in an integer value for the high end of the range. This value
cannot be lower than the low end value. For example, if the user enters 5 for the
low end value and 2 for the high end value then print the message "Number must
be greater or equal to 5.". Repeat this until a valid value is input.
3:Use nested for loops to display all the factors of each integer from the low end to
the high end in increasing order starting with 1. The outer for loop will iterate
from the low end to the high end integer and the nested for loop will display all
the factors for a particular integer. An integer x is a factor of y if y mod x equals
0. Note that 1 and y are always (trivial) factors of y.
4 Sample
Explanation / Answer
THE CODE in C++ :
#include <iostream>
using namespace std;
int main() {
// your code goes here
int mn,mx,i,j;// dclaring the varibles
cout<<"Enter the range for low value";
cin>>mn; //ask input for lower end
if(mn<1){
cout<<" number must be greater than 1"; //display error message
while(mn<1){ //while loop that itreates until positive value enterd
cin>>mn;
if(mn>1){ //if value is grater than 1 exit the loop
break;
}
cout<<" Number must be greter than 1";
}
}
cout<<" Enter the number for high range value";//enter maximum value
cin>>mx;//take max value
if(mx<mn){ //if max value is grater tahn the min value display error message
cout<<" Number must be greater or equal to "<<mn;
while(mx<mn){ //while loop that iterates until appropriate value is entered
cin>>mx;
if(mx>=mn){ //if appropriate value is entered break the loop
break;
}
cout<<" Number must be greater or equal to "<<mn;
}
}
for(i=mn;i<=mx;i++){ //outer for loop that iterates from min value to max value
for(j=1;j<i;j++){//inner for loop that iterateas less tahn the max value
if(i %j==0){ //if the number is factor of given value print the numbewr
cout<<" "<<j<<" is the factor of"<<i;
}
}
}
return 0;
}
GIVEN INPUT :
-1
0
2
1
0
3
EXPECTED OUTPUT :
Enter the range for low value
number must be greater than 1
Number must be greter than 1
Enter the number for high range value
Number must be greater or equal to 2
Number must be greater or equal to 2
1 is the factor of2
1 is the factor of3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.