In this program, You will learn how to check a number is prime or not in R.
Example: How to check a number is prime or not in R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { n = as.integer(readline(prompt = "Enter a number :")) f = 1 i = 2 while (i <= n / 2) { if (n %% i == 0) { f = 0 break } i = i + 1 } if (f == 1) { print(paste("Number is prime :", n)) } else{ print(paste("Number is not prime :", n)) } } |
Output:
1 2 3 4 | Enter a number :7 [1] "Number is prime : 7" |