# Thread: Problems with logic? Homework Question

1. ## Problems with logic? Homework Question

So this a three part problem and I believe I have 2 parts working but when I changed logic statement in loops I end up with problems, I have read and reread every book and web post and still I have some sort of disconnect in my brain with loops and logic statements. Here is what I wrote the last one is not working, it works after I compile it and then stops following the 3 count and at this point I have confused myself and started just moving things around until I got it to stay open as it kept exiting with any key input and wouldn't enter loop at all.

For loop takes user input and controls the number of times a user can enter numbers, if statement tests number for a multiple of for and has a break to exit if true.

Code:
```/*HW3Q1a.c program that ask the user to enter a number that is positive and
a multiple of 4. The user has three attempts to find such a number.*/
#include<stdio.h>

int main(void)
{
//declare variables
int user_num;//input form user
int counter=0;//counter to stop program after 3 tries
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
for (counter = counter+1;counter <= 3; counter++)
{
scanf("%d", &user_num);
printf("\n");
if(user_num<0)
{
break;
}

else if(user_num % 4 == 0)
{

printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);
break;
}
else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}

}
system("PAUSE");
return 0;
}```
This one is the same except it uses a while loop and a ! (not) in statement, program loops until counter hits 3 or a multiple of 4 is entered.
Code:
```/*HW3Q1b.c program that ask the user to enter a number that is positive and
a multiple of 4. The user has three attempts to find such a number.*/
#include<stdio.h>

int main(void)
{
//declare variables
int user_num=-1;//input form user
int counter=0;//counter to stop program after 3 tries
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
while(!(counter >= 3 || user_num %4 == 0))//loop stops when counter hits 3
{
scanf("%d", &user_num);
counter++;
printf("\n");

if(user_num < 0)
{
printf("Only positive numbers, Try again\n");
break;
}

else
{
if(user_num%4==0)
{
printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);

}

else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}

}
}
system("PAUSE");
return 0;
}```
this one is the same except I couldn't use a global !(not) but I wanted to do an inverse statement of the last, I can use a ! but it can't be global. I can not get both conditions to work together. change

Code:
```/*HW3Q1c.c program that ask the user to enter a number that is positive and
a multiple of 4. The user has three attempts to find such a number.*/
#include<stdio.h>

int main(void)
{
//declare variables
int user_num=1;//input form user
int counter=0;//counter to keep track of user attempts
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
while((counter < 3) && (user_num %4 == 1))//loop stops when counter hits 3
{
scanf("%d", &user_num);
counter++;
printf("\n");

if(user_num < 0)
{
printf("Only positive numbers, Try again\n");
break;
}
else
{
if(user_num%4==0)
{
printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);

}

else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}

}
}
system("PAUSE");
return 0;
}```
Thank You for any help
marie

2. Hint: I would probably use a while loop with the call to scanf() contained in the while statement itself and check for its return value along with the 3 checks for the desired conditions (including the count).

3. Thank you for your time I am not sure if you meant what I did I put scanf() in while statement and == compared it to %4==0 and it just put answers into the program and wouldn't terminate. I am really new to all this and struggling to keep up, so I'm guessing that is not what you meant or I had logic wrong again.
again Thank You for your time,
Marie

4. This should give you an idea of what I mean. It only checks that the scanf input is valid and that the number is not negative though. If the input is invalid the input buffer needs to be flushed.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int counter = 0;
int user_num;

fflush(stdout);    // flush output buffer

while(scanf("%d",&user_num) != 1 || user_num < 0 )
{
while(getchar() != '\n');    // flush input buffer
printf("\nInput Error - Please enter a number: ");
fflush(stdout);

}

return 0;
}```

5. btw just to keep your options open, I suggested earlier that you could also check for the count of the input attempts in the same while statement along with your other checks. That's certainly possible, although you will need to think carefully about the logic needed. But in the case of the count, you might end up with cleaner code by instead checking for the count in the body of the while loop and using break to exit when the count limit is reached.

Might be worth while checking out both approaches if you have the time.

6. I think you have a break where you probably want a continue. What happens to the loop when a user enters a negative number ?

7. hello marie, i didn't understood what you write in english . but in coding i understood comment used in first line very well .so according to your question.
program that ask the user to enter a number that is positive and a multiple of 4. The user has three attempts to find such a number.
in first coding you used ((break) statement in wrong place, no need
of this. delete break; statement from following after printf statement in your coding.

Code:
```else if(user_num % 4 == 0)
{

printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);
break;
}
else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}```

In 2nd and 3rd coding i think wrong logic for condition are used .
IN 2ND CODING no need to use !NOT operator. also no need to use((|| user_num %4 == 0)) in condition according
to your question. replace this coding
Code:
```int user_num=-1;//input form user
int counter=0;//counter to stop program after 3 tries
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
while(!(counter >= 3 || user_num %4 == 0))//loop stops when counter hits 3```
with this coding in your 2nd coding

Code:
```int user_num;//input form user
int counter=0;//counter to stop program after 3 tries
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
while(counter < 3)//loop stops when counter hits 3```
if you use ((counter<3 || user_num %4 == 0))

let user enter 4,8,12 ok. after entering 3 values loop has to be end but
that not happen because if user enter another value 28.
means counter=4 first condition false, now ||user_num%4==0 is to checked and i.e true for 28. so program continues until user entered multiple of 4 values. program end if either user enter -ve value or enter value i.e not multiple of 4 otherwise it continuse.

in 3rd coding, also same no need to use (user_num %4 == 0) with && AND operator.