Home > Software engineering >  If m % n == k or m % n == l then print 1, else print any other number
If m % n == k or m % n == l then print 1, else print any other number

Time:09-21

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.

CodePudding user response:

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.

CodePudding user response:

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 ;

CodePudding user response:

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;
}
  • Related