+ 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

31st Aug 2021, 10:45 PM
Praise King
Praise King - avatar
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
1st Sep 2021, 12:45 AM
Tia
Tia - avatar
+ 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).
2nd Sep 2021, 1:35 AM
Marvin
Marvin - avatar
+ 3
So, I made the changes, but is still keeps saying it's wrong.
31st Aug 2021, 11:20 PM
Praise King
Praise King - avatar
+ 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!
1st Sep 2021, 1:12 AM
Tia
Tia - avatar
+ 3
Use BODMAS (Brackets Off Division Multiplication Addition Subtraction) to conrol operator precedence, and use the modulo opearator wherever you need a remainder.🖖
2nd Sep 2021, 6:18 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Thanks!
31st Aug 2021, 11:08 PM
Praise King
Praise King - avatar
+ 2
Rats! It didn't work! Thanks for trying though...
1st Sep 2021, 12:55 AM
Praise King
Praise King - avatar
+ 2
It worked by the way......
2nd Sep 2021, 11:18 PM
Praise King
Praise King - avatar
+ 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; }
31st Aug 2021, 11:21 PM
Praise King
Praise King - avatar
+ 1
Hey Thanks! I'll try it out........
1st Sep 2021, 12:50 AM
Praise King
Praise King - avatar
+ 1
Thanks a lot!
1st Sep 2021, 1:13 AM
Praise King
Praise King - avatar
+ 1
Hey, no problem! Thanks for helping me!
2nd Sep 2021, 10:34 PM
Praise King
Praise King - avatar
0
ااانن
11th Sep 2021, 6:46 PM
Jamal Abdallah
Jamal Abdallah - avatar