0
Help me please. I can not solve the Divisible problem. Tests # 2 and 4 fail.
My attempt: https://code.sololearn.com/cNUsB7IAh8Ba/?ref=app
8 Answers
0
You haven't actually posted a link to the tutorial itself. But I think I know what the problem is. In line no. 10,
if (!(x % 3 && x % 6))
I think you want to check if it is divisible by both 3 and 6. If so then the expression inside the paranthesis checks if x % 3 is not 0 and x % 6 is not 0, and then the ! operator reverses it. So if you remove the !, it should work.
0
Output: no output
0
Тимофей Орловский I am really sorry, you should actually keep the ! operator. The problem is with the paranthesis. It should be
if (!(x % 3) && !(x % 6))
That is, it should check if x % 3 is 0 and x % 6 is zero.
0
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 0;
cin >> x;
if (x % 6 != 0) {
if (!(x % 2 && x % 3)) {
cout << "divisible by all";
}
else {
cout << "not divisible by all";
}
}
return 0;
}
fail #2 and #4
0
Подумайте о ситуациях, когда число делится на 2, а не на 3 или наоборот.
0
При
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 0;
cin >> x;
if (x % 3 != 0) {
if (!(x % 2 && x % 6)) {
cout << "divisible by all";
}
else {
cout << "not divisible by all";
}
}
return 0;
}
//ничего не выводит.
0
Тимофей Орловский did you try replacing
if (!(x % 2 && x % 3)) {
with
if (!(x % 2) && !(x % 3))
0
With the number 789, it should print not Divisible, but it prints Divisible