R Program to check the square root of a number is prime or not


In this program, You will learn how to check the square root of a number is prime or not in R.


Some list of Prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19

Example: How to check the square root of a number is prime or not in R

{
    num = as.integer(readline(prompt = "Enter a number :"))
    n = sqrt(num)
    print(paste("Square root is :", n))

    #Convert to integer now for remove decimal value
    n = as.integer(n)

    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:

Enter a number :49
[1] "Square root is : 7"
[1] "Number is prime : 7"