A problem when running a While Activity

Hi, still here. Now when running a While problem to find prime numbers, the program tells me that 0 and 1 are prime numbers. Otherwise, the results are right. What am I missing?

Hi @Hallgrimur_Ingolfsson_HI

  1. Create a Counter variable(int32)… Counter=2
  2. create a variable(int32) (NumberToCheck)
  3. IsPrime (Boolean)

For example
NumberToCheck=13

While
While (counter <= Math.Sqrt(numberToCheck))
If (numberToCheck Mod counter = 0)
Assign: isPrime = False
Break
Else
Assign: counter = counter + 1

If (isPrime)
// It’s a prime number
Log Message: “The number " & numberToCheck & " is prime.”
Else
// It’s not a prime number
Log Message: “The number " & numberToCheck & " is not prime.”

Hope this Helps

Hi @Hallgrimur_Ingolfsson_HI

Take i=1 Then Take the input from the use save it in number variable


Checks if the number is divisible by i . If yes, increments the counter c

After the loop, checks if c is equal to 2, and logs the result prime or not prime

PrimeOrNot.zip (2.7 KB)

Hope this Helps :slight_smile:

@Hallgrimur_Ingolfsson_HI

Use a if condition and ignore checking when the value is 0 or 1 as mod of 0 or 1 with any number will be 0

Cheers

Thanks Anil, problem solved.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.