Hi guys, i need some help, i have the code to test most numbers but how do i cha
ID: 3797012 • Letter: H
Question
Hi guys, i need some help, i have the code to test most numbers but how do i change it to test 0 and -10? thanks in advance
Allow a user to input an integer and then display only those integers between -20 and 40 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 numbers evenly divisible by 10: -20 -10 0 10 20 30 40
Their sum is: 70
Note that in above output 10 (underlined/in bold) should change according to the user input.
Test your program with the following inputs
a. 10
b. -10
c. 0
d. 7
e. 19
#include <iostream>
using namespace std;
int main()
{
int userNum, sum = 0, i = -20;
cout << "Enter a # between 1 and 10";
cin >> userNum;
cout << "Here are numbers evenly divisible by" << userNum << endl;
while (i <= 40) {
if (i%userNum == 0)
cout << i;
sum = sum + i;
i = i + userNum;
}
cout << "Their sum is" << sum << endl;
system("pause");
return 0;
}
Explanation / Answer
Hi
I have modified the code and highlighted the code changes below.
#include <iostream>
using namespace std;
int main()
{
int userNum, sum = 0, i = -20;
cout << "Enter a # between 1 and 10: ";
cin >> userNum;
cout << "Here are numbers evenly divisible by " << userNum << ": ";
while (i <= 40) {
if (i%userNum == 0){
cout << i<<" ";
sum = sum + i;
}
i = i + 1;
}
cout <<endl<< "Their sum is " << sum << endl;
system("pause");
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter a # between 1 and 10: 10
Here are numbers evenly divisible by 10: -20 -10 0 10 20 30 40
Their sum is 70
sh-4.2$ main
Enter a # between 1 and 10: 7
Here are numbers evenly divisible by 7: -14 -7 0 7 14 21 28 35
Their sum is 84
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.