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