This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
Closed 6 years ago.
im having trouble using multiple operators while programing a simple FizzBuz in C
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
if (0 < n < 10000){
for (int i = 1; i <= n; i ++) {
if (i % 15 == 0) {
puts("TikTak");
}
else if (i % 3 == 0) {
puts("Tik");
}
else if (i % 5 == 0) {
puts("Tak");
}
else {
printf("%d\n", i);
}
}
}else{
printf("-1");
}
}
Now the "if (0 < n < 10000)" comparison operators is ignored for some reason, but if I rewrite it as:
if (n < 10000 && n > 0){
it will work as intended.
Am I missing something? Ill be honest Im newbie in C programing. haha
EDIT: Thanks everybody, haha that was quite simple. I thought that might be the issue I just waned to make surebecause "0 < n < 10000" is litteraly how the assigment says it should look like.
Again, thanks!
Rewrite this condition
if (0 < n < 10000){
like
if (0 < n && n < 10000){
Otherwise the original condition looks like
if (( 0 < n ) < 10000){
and the result of the expression 0 < n is either 1 or 0. So in fact you are comparing 0 or 1 with 10000.
From the C Standard (6.5.8 Relational operators)
6 Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false.107) The result has
type int.
The expression 0 < n < 10000 is equivalent to (0 < n) < 10000 which means you check if 0 < n is less than 10000, which it will always be (the result of a comparison like 0 < n will be zero or one).
Related
output is 0 when entered 1001 or something greater than it.
it should give 0 when the number isn't abundant and should exit if entered number is greater than the limit ,have tried using goto exit.
#include <stdio.h>
void main()
{
int n;
printf("Enter the number\n");
scanf("%d", &n);
int i, sum = 0;
if (1 <= n <= 1000)
{
for (i = 2; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
(sum > n) ? printf("1") : printf("0");
}
else
return;
}
The condition in this if statement
if (1 <= n <= 1000)
is equivalent to
if ( ( 1 <= n ) <= 1000)
the result of the sub-expression 1 <= n is either 0 or 1. So this value is in any case less than 1000.
From the C Standard (6.5.8 Relational operators)
6 Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false. The result has type
int.
You need to write
if (1 <= n && n <= 1000)
using the logical AND operator.
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
Please explain to me why this code is wrong for the task and below I have explained all the four conditions -[][1]
#include stdio.h
int main()
{
int n;
scanf ("%d", &n); //taking input
if (n / 2 != 0)
{
printf ("Weird"); //checking first condition
}
else if (n % 2 == 0 && 2 <= n <= 5)
{ //checking second condition
printf ("Not Weird");
}
else if (n % 2 == 0 && 6 <= n <= 20)
{ //checking third condition
printf ("Weird");
}
else if (n % 2 == 0 && n > 20)
{ //checking fourth condition
printf ("Not Weird");
}
else
{
printf ("Error");
}
return 0;
}
this is the image for the question[1]: https://i.stack.imgur.com/OtY7o.png**
Testing for Odd or Even
n / 2 != 0 does not test whether n is odd. n/2 calculates the quotient that results from dividing n by 2 (rounding any fraction down). So 0/2 is 0, 1/2 is 0, 2/2 is 1, 3/2 is 1, 4/2 is 2, and so on. So n / 2 != 0 is true for all n other than −1, 0, and 1.
To test whether a number is odd, you can use n % 2 != 0. n%2 calculates the remainder from the division. If it is zero, n is even. If n is not zero, n is odd.
Using Else Efficiently
Once you have tested whether n is odd using n % 2 != 0, you do not have to test whether it is even in the else clauses. The else expressions and their statements will be evaluated only if the if expression is false, which happens (after the correction above) only when n is even. So we do not need to test again.
Testing For an Interval
In C, 2 <= n <= 5 does not test whether n is between 2 and 5. It is parsed as (2 <= n) <= 5. This is evaluated by comparing 2 to n, which produces 0 (if false) or 1 (if true). This result, 0 or 1, is then used in … <= 5. Since 0 and 1 are both less than or equal to 5, the result is always 1 (for true).
To test whether n is greater than or equal to 2 and less than or equal to 5, you must write this out explicitly: 2 <= n and n <= 5, which we join with the “and” operator, &&: 2 <= n && n <= 5.
Other Issues
The proper form for including stdio.h is #include <stdio.h>, not #include stdio.h.
A proper declaration for main is int main(void), not int main().
Corrected Program
A program with these issues corrected is:
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n); //taking input
if (n % 2 != 0)
{
printf ("Weird"); //checking first condition
}
else if (2 <= n && n <= 5)
{ //checking second condition
printf ("Not Weird");
}
else if (6 <= n && n <= 20)
{ //checking third condition
printf ("Weird");
}
else if (n > 20)
{ //checking fourth condition
printf ("Not Weird");
}
else
{
printf ("Error");
}
return 0;
}
There is a typo in the line:
if (n / 2 != 0)
The compiler will not complain, but you will get unexpected results at run time.
Here you meant to check if the remainder of division by 2 is not equal to zero (i.e.: modulus operator), and not the division by 2. This line should be
if (n % 2 != 0)
Second thing: you can't tell C to compare values in ranges like this 2 >= n >= 4. You will have to split the comparison into 2 comparisons. This line:
else if (n % 2 == 0 && 2 <= n <= 5)
Should be:
else if (n % 2 == 0 && 2 <= n && n <= 5)
You will need to fix all the lines that have this comparison as well.
This question already has answers here:
Why is a condition like (0 < a < 5) always true?
(4 answers)
Closed 9 years ago.
I have this question & in the answer it says that due to left to right associativity the result would be 1 that is true for this statement. This is the code.
#include<stdio.h>
int main ()
{
int i=0,x=10,y=10,z=5;
i=x<y<z;
printf("\n\n%d",i);
return 0;
}
But x is greater than z here so how is this happening ?
The expression x
(x < y) < z
so it becomes
(10 < 10) < 5
which further is evaluated into
0 < 5
which is true.
I think you wanted something like this:
x < y && y < z
Because of operator precedence and associativity
i = x < y < z;
is parsed as:
i = ((x < y) < z);
After substituting the variable values, this becomes:
i = ((10 < 10) < 5);
Since 10 < 10 is false, this becomes:
i = (0 < 5);
Since 0 < 5 is true, that becomes:
i = 1;
x<y<z is not a single valid expression. Instead it evaluates x<y first (operator precedence is done left to right here) as true/false (false in this case as they're equal), converts it to an int value of 0, and then compares this value with z.
Use (x < y && y < z) instead.
It first evaluates x < y which is false (0), then 0 < z which is true (1).
WHat C compiler does is, in x<y<z;
starts from left, so as x is not less than y therefore it replaces that expression with '0'
so it becomes 0<z and as that is true. it set the variable to 1.
I have this part of an if statement and I'm getting a weird output.
int x = 10;
if(1 < x < 5){
printf("F\n");
}
Why does it print "F"? Logically isn't the if statement false because x is greater than 1 but not less than 5?
In C, you can't chain comparisons like that. The expression 1 < x < 5 is evaluated as (1 < x) < 5: so for x = 10, the expression is (1 < 10) < 5. (1 < 10) is true, which C represents as the value 1, so the expression reduces to 1 < 5. This is always true, and your printf() if executed.
As level-999999 says, in C you need to explicitly combine single comparisons with && and ||.
If you are using C, you should have broken down the condition into two arguments :
if ( x > 1 && x < 5) {
printf("F\n");
}
I'm very new to this, and i'm trying to create a text based minesweeper.
I want the player to decide how big he want the grid to be.
My problem is, that the if-statement, that should make sure, the user types in a number from 1 to 10 doesn't work. Please have a look.
scanf ("%i/%i",&x,&y);
if (0 < x < 11 && 0 < y < 11)
{
printf ("you have selected %i by %i\n",x,y);
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
grid[x][y] = 'O';
printf ("%c ", grid[x][y]);
}
printf ("\n");
}
}
else
printf ("Wrong gridsize");
C does not support double comparisons like:
0 < x < 11
you should write instead
0 < x && x < 11.
It may be misleading, because the first statement is syntaxically correct (it compiles), but it does not do what you may believe: check both boundaries like in a mathematical expression (what python does for instance).
It's like if you had written
(0 < x) < 11
The first binary expression returns a boolean (well, really an int in C, a boolean in C++). This boolean once casted to int is 0 or 1, always below 11, henceforth the expression is always true.
Of course the same is true for checking y boundaries. Now you should be able to fix the problem by yourself.
The if statement has to be like
if ((x > 0 && x < 11) && (y > 0 && y < 11))
you have written wrong if statement.
Here is correct form.
if ((x > 0 && x < 11) && (y > 0 && y < 11))
Here are the relational operators
> greater than 5 > 4 is TRUE
< less than 4 < 5 is TRUE
>= greater than or equal 4 >= 4 is TRUE
<= less than or equal 3 <= 4 is TRUE
== equal to 5 == 5 is TRUE
!= not equal to 5 != 4 is TRUE
C does not support double comparisons.
In your scanf statement remove / operator change it to
scanf ("%i%i",&x,&y);
And what your if statement is doing is what you expect to do
if (0 < x < 11 && 0 < y < 11)
first when you enter the value x and y (x = 4 and y = 6)
It checks if x is greater than 0 (which is true ) so 1 is substituted in place of 0 < x
now its something like this for compiler 1 < 11
next it checks that which is also true
similarly for y whichever value you enter will always be true.
0 < x < 11
means
(0 < x)<11. If x is 5, 0 < x will be 1 (true). Next evaluation will be 1 < 11, that will be true so the result is true.
But, if x = 20, 0<20 is 1, 1<11 is true as well, but you would expect a false result.