Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
study question
How could I write a program that asks for 5 values and outputs the percentage of values that are evenly divisible by 4 (or any other number)?
11 Answers
+ 2
include <iostream>
using namespace std;
int main () {
float percentage; int val1, val2, val3, val4, val5;
float count = 0;
cout << "Input 5 values" << endl;
cin >> val1 >> val2 >> val3 >> val4 >> val5;
if (val1 % 4 == 0){
count++;
}
if (val2 % 4 == 0){
count++;
}
if (val3 % 4 == 0){
count++;
}
if (val4 % 4 == 0){
count++;
}
if (val5 % 4 == 0){
count++;
}
percentage = (count/5)*100;
cout << "Percent of numbers evenly divisible by 4: " << percentage << endl;
return 0;
}
+ 2
use modulus. if the result is 0, counter++. Then divide (counter / total values) * 10
NOTE: 0 means no remainder
+ 1
I still get the output 0 with the float
+ 1
thanks
0
alright so would I have to set up if statements for each of the values?
0
yea or put each value in a container, loop through the container, and if only once
0
still not seeing what's wrong
0
percentage should be of type float or else it would always be 0
0
float count. then finally multiply by 100 not 10 (sorry)
0
You're welcome. Happy to help!