How to interpret a conjunction as a loop condition? [closed] - while-loop

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I have a loop that doesn't exit and I need help converting a problem statement into a loop statement. Problem statement attached here:
#include <stdio.h>
int main(){
int num,last,newnum;
printf("ENTER YOUR NUMBER: ");
scanf("%d",&num);
while (num != 7 || num != -7 || num != 0){
last = num % 10;
last = last * 2;
num = num / 10;
num = num - last;
printf("%d",num);
}
}

You used the || operator when the && operator is the intended one. Let's look at why:
Say num == 0, then the loop should not execute but instead becomes an infinite loop.
If we solve the boolean condition like so:
true || true || false evaluates to true. The only case where it would evaluate to false would be if num equalled 7, -7, and 0 simultaneously, which is impossible. Plugging a sample value into the boolean condition is a good tool for figuring out why your loop ends too early or goes on too long.
while (num != 7 && num != -7 && num != 0)
This ^ is what your loop should be. However, as I was testing it, it seemed to get stuck in infinite loops regardless. That is because the loop body won't always result in 7, -7, or 0, as not every number is divisible by 7. The number 499443, divisible by 7, didn't produce an infinite loop, but the number 2, which is not, did.
Also, indent your code in the future, as it can make debugging easier.
If you need a more practical way to check if a number is divisible by 7, then you can use:
if (num % 7 == 0) {
// Divisible by 7
}

Related

While Loop: Challenge for beginners

Challenge:
Use a while loop to increment count by 2 on each repetition of the block of code. Run the code block of your while loop until count is 8
let count = 2;
I just started learning code two days ago, I am not really sure the logic behind what it is I should do, when to use a while loop or logically the difference between that and an infinite loop. I would really appreciate any help
You call while with a condition, in your case while count < 8.
Inside your while loop you increment count by two every iteration until it is 8 or bigger. So it would look like this:
let count = 2;
while (count < 8) {
count = count + 2;
}
When the condition is not true, so when your count is equal or bigger than 8, the inner part of the while loop is not being executed (your while loop terminates).
An example for an infinite while loop would be with the condition "true":
while (true) {
...
}

Loop is not doing what expected

I'm studying Obj-C and now I'm not understand why my loop isn't work how it should. I know a way i could achieve result with single while loop, but i want to do this through do while and can't figure out whats going wrong.
What i want is, to show integer called triangularNumber for integers 5,10,15.. and so on. There is what i've tried:
for (int i=1; i<51; i++){
int triangularNumber;
do {
triangularNumber = i * (i+1)/2;
NSLog(#"Trianglular number is %i", triangularNumber);
}
while (i%5 == 0);
}
It produce odd results:
1) Condition of i%5==0 is not met, it output 1,3,6,10 then infinite numbers of 15
2) It create an infinite loop
Please tell me, what is wrong in that code and how to fix it. Thanks!
Instead of do while loop within for loop, use if to check whether a number is divisible by 5 or not.
If you want to do it with do while loop, you could do the following:
int i = 5;
int triangularNumber;
do {
triangularNumber = i * (i+1)/2;
NSLog(#"Trianglular number is %i", triangularNumber);
i += 5;
} while (i < 51);
Reason your code is not working:
Within your do while loop, if i say is 5, then you will end up running in infinite loop as you will satisfy your do while loop's condition which is i%5==0
You just need numbers like 5, 10, 15.. so it's just matter of one loop. Now loop starts with 1 till 50, and you are picky in the sense you just need one element of 5, hence to be picky you could use if condition which says print if and only if my i is multiple of 5.
Do while will always enter into loop and will execute it once. So for 1 it will enter and do calculation for triangularNumber as 1 * 2 /2 = 1 and hence your output 1 and checks condition post printing is 1 divisible by 5, no then it comes out and increments i to 2 and follows same routine as above.

Checking to see if a number is divisible [duplicate]

This question already has an answer here:
Modulo operator in Objective C
(1 answer)
Closed 8 years ago.
I want to check if a number is divisible by 6 and if not, I need to increase it until it becomes divisible.
Use the modulus operator. This operator returns the remainder of the division operation. Check if this is 0 to check if it divides perfectly.
if (i % 6 == 0) {
// ...
}

Looping until multiple conditions reached in Objective-C

So i am trying to create a program that can find a number that can be divided by numbers 1-20. I know that i will have to use the following simple code concepts:
I know how loops work and how to create a loop that runs until a condition is met. Is there a simple was to run a loop until several conditions are met?
while ( condition1 && condition2 && condition3... ) {}
or
for ( int i = 0; i < n && condition1 && condition2... ) {}
Obviously these will loop while the conditions are true, not until the conditions are met. Its a simple change in the logic though to get the result you want
EDIT
Ane example of the kind of loop youre looking for could be like:
int number = ...;//initialized somewhere, this is what we're checking
BOOL divisible = YES;
for ( int i = 1; i <= 20 && divisible; ++i )
{
if ( (number % i) != 0 )
divisible = NO;//not divisible by i
}
Good answers in play, but I think it's good to mention the break operator in this discussion. Any loop, at any time, can be terminated using this operator. This can be helpful if you do not know all of the parameters which might go out-of-bounds, and you want to have a way of breaking the loop for reasons you may not have explicitly anticipated (i.e. perhaps your connection to a resource is no longer available...)
NSError *error = nil;
while(true) {
// run your app
if(error) {
break;
}
}
If a number is divisible by all numbers from 1 to 20 then it is divisible by the LCM of 1 to 20 so divisibility test is if(!(n%232792560)).
Further if m = pq | n then p|n, q|n so to explicitly test you only need to check for divisibility by primes. i.e if the number is not even then there is no need to check for divisibility by 4, 6, 8, 10, 12, 14, 16, 18 or 20. This reduces the test to the number being congruent to the 8th primorial = 9699690
OK, perhaps on second reading not as explicit as I should like: the expanded test looks like (by de Morgan's theorem)
if(!(n%19 || n%17 || n%16 || n%13 || n%11 || n%9 || n%7 || n%5))
// number is divisible by 1..20

What will be the value of i in the following pseudocode?

I got this question in a programming test. Do you think this question is even correct? Look at the answer choices. (2^x means 2 raised to x)
Consider the following pseudocode.
x := 1;
i := 1;
while (x >= 1000)
begin
x := 2^x;
i := i + 1;
end;
What is the value of i at the end of the pseudocode?
a) 4
b) 5
c) 6
d) 7
e) 8
I am sure that the value of i will 1. I told the examiner of the discrepancy and he advised me the leave the question unanswered if I felt it was incorrect. What else could I have done?
1
X < 1000, so it doesn't enter the while.
Or there is an error in the Question (and X should be <= 1000 and not >=1000)
If it's <= 1000 it should be 5:
2 - 4 - 16 - 65K
2 - 3 - 4 - 5
This question tests two things:
can you read code
can you communicate / interact
Since you asked about the discrepancy, you showed 1. to be true. I'm not so sure if you passed 2, it depends too much on the situation / expectations.
I believe I would have left a note on the answer sheet stating 'none of the given'.
Not an easy situation!
As written, the answer would be 1.
Had the test on the while been reversed (i.e. x < 1000), then the series is:
At the end of each loop iteration
i = 2, x = 2
i = 3, x = 2^2 = 4
i = 4, x = 2^4 = 16
i = 5, x = 2^16 = 65,536
So i would be 5
If I where you, I would say that it is none of the above and basically say that since x is less than 1000 when the while loop starts, the value of i is never modified. I think that it is a bad thing to leave blank answers in a quiz, it is always better to write something which you think is relevant. If you think that there is a mistake, you can always state an assumption before stating your answer, so in this case you can either say that the while loop never works, or else, you explicitly state an assumption, in this case, it would be something like "Assuming that there is a mistake in the question, and that "while (x >= 1000)" should in fact be "while (x <= 1000)"...
Then, you proceed with your working.