I am currently learning about While Loops in my C++ class, and while this has been mostly no issue for me, I am having terrible trouble creating the following program:
__________________________________________________________________________
"Allow a user to input an integer and then display only those integers between -30 and 50 inclusive that are evenly divisible by the user entered value. Also display the sum of such numbers. Say user inputs 10. Then you need to display below:
'Here are the numbers evenly divisible by 10: -30 -20 -10 0 10 20 30 40 50
Their sum is: 90'
Note that in above output 10 should change according to the user input."
__________________________________________________________________________
#include <iostream>
usingnamespace std;
int main() {
int numMain;
int i = -30;
cin >> numMain;
while ((i >= -30) && (i <= 50)) {
if (i % numMain == 0) {
cout << i << endl;
}
i++;
cout << "Here are the numbers evenly divisible by " << i << " :" << endl;
}
cout << "Their sum is: " << endl;
system("pause");
return 0;
}
#include <iostream>
usingnamespace std;
int main() {
int numMain;
int i = -30;
int runningTotal{ 0 }; // Variable to maintain the running total.
cin >> numMain;
cout << "Here are the numbers evenly divisible by " << numMain << ": ";
while ((i >= -30) && (i <= 50)) {
if (i % numMain == 0) {
runningTotal += i;
cout << i << " "; //endl;
}
i++;
//cout << "Here are the numbers evenly divisible by " << i << " :" << endl;
}
cout << "\nTheir sum is: " << runningTotal << endl;
system("pause");
return 0;
}
Output:
10
Here are the numbers evenly divisible by 10: -30 -20 -10 0 10 20 30 40 50
Their sum is: 90
Press any key to continue . . .