-2

4 integers are given (all no more than 10^6): m, n, k, l. If m % n == k or m % n == l, then print 1, else any other number. Conditional operators cannot be used!

Examples:

12 8 3 4 // input
1 // output

0 5 1 2 // input
0 // output

I wrote this code:

#include <iostream>

using namespace std;

int main()
{

int m, n, k, l;
cin >> m >> n >> k >> l;

cout << ((1 / (((m % (n + 1 / (n + 1))) - k) * ((m % (n + 1 / (n + 1))) - k) + 1)) - 1) * ((1 / (((m % (n + 1 / (n + 1))) - l) * ((m % (n + 1 / (n + 1))) - l) + 1)) - 1) + 1;

return 0;
}

But it does not work for all cases. For example, 0, 0, 0, 0 gives 1, but should give any other number. Please help.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
boboc5
  • 1
  • 1
  • The requirement is unclear. The conditional operator is `?:`. Do you perhaps actually mean boolean operators like `and` and `or` ? – 463035818_is_not_an_ai Sep 21 '22 at 10:17
  • 5
    Is this code for an obfuscation challenge? –  Sep 21 '22 at 10:19
  • 2
    0%0 (or anything `%0`) is mathematically undefined, because Modulo implies a division, meaning you actually 'divide by zero' with these parameters. With integers, your program should crash, with floating point numbers, you should get 'Infinity' (https://stackoverflow.com/questions/51474239/c-warning-division-of-double-by-zero). – Refugnic Eternium Sep 21 '22 at 10:29
  • 3
    Do not vandalize your posts. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/) for as long as it sees fit to do so. For alternatives to deletion, see: [I've thought better of my question; can I delete it?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – Cody Gray - on strike Sep 22 '22 at 07:35

3 Answers3

1

Note that there is no answer for n == 0 because division by zero is undefined.

In the other cases, since true prints as "1" and false as "0" by default,

cout << (m % n == l || m % n == k);

should do it.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

what i understand from your question you are checking for 2 conditions, so after taking the input from the user we can check those conditions and assign value of val integer to 1 if that condition is true, else it will be 0 as initialized.

int m, n, k, l;
int val =0;
cin >> m >> n >> k >> l;

if(m % n == k || m % n == l){
 val = 1;
}
cout << val ;
-1

From what I understand I think you are trying to compare the modulus of two numbers with the other two inputs and if the result matches you want 1 as output otherwise 0

this will help you achieve it

#include <iostream>

using namespace std;

int main()
{

int m, n, k, l;
cin >> m >> n >> k >> l;

if(m%n == k || m%n == l)
{
  count << 1
}
else 
{ 
  count << 0
}
return 0;
}
Usama Arslan
  • 111
  • 4
  • Besides the requirements that "[c]onditional operators cannot be used!" likely also applies to `if` statements, code-only answers are highly discouraged as they tend to promote [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming), which is bad. You also don't really tell the OP what was wrong with the code in the question, or how your code would solve that problem. Please read about [how to write good answers](https://stackoverflow.com/help/how-to-answer). – Some programmer dude Sep 21 '22 at 10:46
  • Thanks @Someprogrammerdude just updated the answer – Usama Arslan Sep 21 '22 at 10:52