This is for c++. Write a program that allows the user to repeatedly enter intege
ID: 3759138 • Letter: T
Question
This is for c++. Write a program that allows the user to repeatedly enter integer values. For integer values greater than 0 the program will display the factors of the value.
Write a program that allows the user to repeatedly enter integer values. For integer values greater than 0 the program will display the factors of the value. The factors of a value are integers that evenly divide the value When the user enters a value of zero the program ends. If the user enters a negative value an error message is displayed. Hint: First write something to find the factors of a positive integer entered by the user, then worry about repeatedly getting values from the user. Sample output from a program that satisfies the requirements of this exercise is given below Example #1 Enter an integer value greater than zero and I will calculate its factors or enter a 0 to end this program: 100 The factors of 100 are: 1 2 4 5 10 20 25 50 100 Enter an integer value greater than zero and I will calculate its factors or enter a 0 to end this program: -150 ERROR: -150 is not a valid entry. Enter an integer value greater than zero and I will calculate its factors or enter a 0 to end this program: 150 The factors of 150 are: 1 2 3 5 6 10 15 25 30 50 75 150 Enter an integer value greater than zero and I will calculate its factors or enter a 0 to end this program: 0Explanation / Answer
#include <iostream>
using namespace std;
int main() {
while(true) {
cout<<"Enter an integer value greater than zero and I will calculate its factors or enter 0 to end this program: ";
int in;
cin>>in;
if (in < 0) {
cout<<" ERROR: "<<in<<" is not a valid entry. ";
continue;
}
if (in == 0) {
return 0;
}
cout<<" The factors of "<<in<<" are: ";
for (int i=1; i<=in; i++) {
if(in%i == 0)
cout<<i<<" ";
}
cout<<" ";
}
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.