+ 4
How do I use % (modulo) to see if a variable is a multiple of a specific number?
I am stuck on the 2nd challenge in the C++ course
15 Antworten
+ 6
Praise King
Hi Praise King. If I'm checking for n being a multiple of 5, I'd use the statement ' if (n % 5 === 0) { '. Or (==) whichever's appropriate.
For the nested loop could you try this?
do {
if (n % 5 === 0) {
cout....Beep...;
} else {
cout .... n--... ;
}
} while (n > 0)
Not sure you need the ' return n ' but Java, Javascript and Python are my main languages. BEST WISHES
+ 4
A number x is a multiple of another number y if x is divided by y returns a remainder of zero. The modulus operator (%) represents the remainder, and thus to check if x is a multiple of y, test if x % y == 0 (codewise).
+ 3
So, I made the changes, but is still keeps saying it's wrong.
+ 3
Sure programming is all about collaboration. The majority of my work day is sharing code bits and answering questions with teammates.
Another tip that helps us, is to break the solution into small pieces and print them to see if we're on the right track. Instead of changing and running the whole code, just take the small "if" section (for ex) through a tiny " do loop" and see if it prints correctly. If so then YAY!, tackle the next section. Remember to do this in the coding playground -- you're gonna be a C++ pro in a few days!
+ 3
Use BODMAS (Brackets Off Division Multiplication Addition Subtraction) to conrol operator precedence, and use the modulo opearator wherever you need a remainder.🖖
+ 2
Thanks!
+ 2
Rats! It didn't work! Thanks for trying though...
+ 2
It worked by the way......
+ 1
This is the code:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
//your code goes here
do {
cout << n-- << endl;
} while (n > 0);
if (n % 5) {
cout << "Beep" << endl;
}
else {
cout << n-- << endl;
} while (n > 0)
return 0;
}
+ 1
Hey Thanks! I'll try it out........
+ 1
Thanks a lot!
+ 1
Hey, no problem! Thanks for helping me!
0
ااانن