How to write a while statement in c? - c

I should write a C while statement that continues while x is both larger than 10 and a multiple of either 6 or 7. I only need to include the actual while statement, not the entire loop!
I know it may look funny to you, but this is what I have so far.
#include<stdio.h>
int main(){
int x;
while (x > 10, x % 7 = 0 || x % 6 = 0) {
printf("%d\n", x);
}
}

Logical and is &&. As an operator, , is "ignore the result of the thing to the left".

Your Boolean logic and comparison both use the wrong syntax. It should be like this:
while (x > 10 && (x % 7 == 0 || x % 6 == 0)) {
The comma (,) is not a valid Boolean operator. What you wanted is && (logical AND). You need parentheses around the other two expressions because AND (&&) has higher precedence than OR (||).
And = is for setting values; == is for comparing them.
Finally, as others pointed out, you don't set x anywhere in the code that you have posted. So, your loop will not run. And you don't modify x in the loop, so, if you ever get into the loop, you will never get out.

The program can look like
#include <stdio.h>
int main( void )
{
int x;
printf( "Enter an integer number: " );
scanf( "%d", &x );
while ( x > 10 && ( x % 7 == 0 || x % 6 == 0 ) )
{
printf( "%d\n", x );
printf( "Enter next integer number: " );
scanf( "%d", &x );
}
}
Or you could enlarge the condition in the while loop the following way (to check that the input is correct)
#include <stdio.h>
int main( void )
{
int x;
printf( "Enter an integer number: " );
while ( scanf( "%d", &x ) == 1 && x > 10 && ( x % 7 == 0 || x % 6 == 0 ) )
{
printf( "%d\n", x );
printf( "Enter next integer number: " );
}
}
As for your condition in the while loop then it has two errors
while (x > 10, x % 7 = 0 || x % 6 = 0)
It uses the comma operator instead of logical operator && and it uses the assignment operator as for example x % 7 = 0 instead of the comparison operator x % 7 == 0. Also variable x was not initialized.

Your while statement should read
while (x > 10 && (x % 7 == 0 || x % 6 == 0))
Note the use of && for "boolean and", and == for "equality comparison".

I should Write a C while statement that continues while x is both
larger than 10 and a multiple of either 6 or 7. I Only need to include
the actual while statement, not the entire loop!
Let's analyze the condition that will go in the while() statement:
x is both larger than 10 and a multiple of either 6 or 7
Can be expressed as
x is greater than 10 AND a multiple of 6 OR multiple of 7
So let's code that in C:
x is greater than 10
x>10
AND
&&
x multiple of 6 OR x multiple of 7
(x%6)==0 || (x%7)==0
Then, all together
x>10 && ( (x%6)==0 || (x%7)==0 )
So your while sentence is like this:
while (x>10 && ( (x%6)==0 || (x%7)==0 ))
{
stuff...
}

Here can be the Solution Ava
while (x > 10 && (x % 7 == 0 || x % 6 == 0)) {

Related

my do while loop isnt meeting both requirements

When I am trying to get the input for my variable, it is only meeting one of the requirements (ie: the < 1 requirement) and skips the other requirement even though im using the && operator.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x;
do {
x = get_int("what is the height of the pyramid?:");
} while (x > 0 && x < 8);
printf("%i", x);
}
I tried just using the x < 8 for the requirement but it still went through when I entered 9, 10, 11 etc.
If you want x to be between 0 and 8 (both ends exclusive), then you need to repeatedly ask for input when this condition is not satisfied.
In other words, when x is outside this range it means x is less than or equal to 0 OR greater than or equal to 8.
That said, I believe the proper input range for that problem set is actually 1-8 (both ends inclusive):
do {
x = get_int("What is the height of the pyramid?: ")
} while (x < 1 || x > 8);
The test is exactly the opposite of your intent. The do/while condition should test a condition for repeating the input and write while (!(x > 0 && x < 8)); or equivalently: while (x < 1 || x >= 8);
It is unclear what your requirements are, but it seems the number should be between 1 and 7 inclusively. If 8 should be included, the test should be modified as while (!(x > 0 && x <= 8)); or equivalently: while (x < 1 || x > 8);
do/while loops are often confusing and error prone. I suggest using a for(;;) aka for ever loop and break statements when conditions are met:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x;
for (;;) {
x = get_int("What is the height of the pyramid? ");
if (x == INT_MAX) {
printf("Input error or end of file\n");
return 1;
}
if (x > 0 && x < 8) {
break
}
printf("The height should be between 1 and 7\n");
}
printf("%i\n", x);
return 0;
}

How to add multiple parameters in DO-WHILE loop in C

#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
do
{
n = get_int("height: ");
}
while(n<1&&n>8);
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("#");
}
printf("\n");
}
}
im writing a program to print a square with hashes,but my while in the Do loop wont work.
i want it to accept values only between 1 and 8 inclusive,but it wont work and wouldnt prompt again if i enter values out of the parameter.but it works if I only put a single parameter in the while loop e.g. n<1.
please help me,im a beginner.
The answer is very easy and it is not related to the programming only simple math and logic
while(n<1&&n>8)
If n is lower than 1 it cannot be larger than 8 at the same time.
Reason for not working : while( n<1 && n>8); See the image for clarity. There is no value in the intersection. I mean there is no number, less than one and greater than 8.
Solution : Use while(n < 1 || n > 8); for n values between 1 and 8 inclusive.
There is no such a number that would be simultaneously less than 1 and greater than 8.:)
n<1&&n>8
A simple way to write correctly the condition of the loop is to write at first the condition that a number shall satisfy. That is
1 <= n && n <= 8
and then to apply negation to the condition
do { /*...*/ } while ( !( 1 <= n && n <= 8 ) );
Then using rules for logical operations you can write for example
do { /*...*/ } while ( !( 1 <= n ) || !( n <= 8 ) );
and at last
do { /*...*/ } while ( ( 1 > n ) || ( n > 8 ) );
or
do { /*...*/ } while ( n < 1 || n > 8 );

scanf does not ask for input

It takes a lot of time to compile, and some random number shows up, apparently the scanf() doesn't ask for the input
#include <stdio.h>
int main() {
int a;
//a = 1472464;
scanf ("%d", &a);
if ((a % 6) && (a %4) == 0)
{
printf("Input %d is divisible by 6 and 4\n", a);
}
else {
printf(" Input %d is not divisible by 6 and 4\n", a);
}
printf("Hello, World!\n");
return 0;
}
This line is wrong:
if ((a % 6) && (a %4) == 0)
It should be:
if ((a % 6) == 0 && (a %4) == 0)
I don't see any other obvious problem with the code.
The expression (a % 6) && (a %4) == 0 does not compare both modulo-operations with zero. Instead it does (a % 6) which will result on a number between 0 and 5, and use that as a boolean value that it then uses with the result of (a %4) == 0.
Instead you need to do each comparison separately: (a % 6) == 0 && (a % 4) == 0
The important thing to know here is that in C only zero and a null pointer is considered "false". Anything that is not zero (or a null pointer) is true.
That means that if a for example is 4 then a % 6 will be "true" since a % 6 is 4 which is not zero. Conversely when a is for example 6 then a % 6 will be 0 which is "false".
So using only a % 6 will actually give the opposite result to what you want, it will be "true" when a is not evenly dividable by 6.

How to compare number against an interval? ( x > (1,10))

How to compare one number against an interval.
Is there any way to shorten this expression:
if ((x%10) == 1 || (x%10) == 2 || (x%10) == 3 || (x%10) == 4 ) // ... till number 9.
Assuming x is an integer, there are only 10 possible values. And the only one not checked, 0, is also the only false integer.
if (x % 10)
{
...
You can check it like this:
if(!(x%10)) {
}

If statement doesn't stop execution. C programming

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.

Resources