Computer Science, asked by alpha01234, 2 months ago

WAP to check whether square root of a given number is prime or not​

Answers

Answered by cuteprincess200012
2

Answer:

Python Program for prime number

Initialize a for loop starting from 2 ending at the integer value of the floor of the square root of the number. Check if the number is divisible by 2. Repeat till the square root of the number is checked for. In case, the number is divisible by any of the numbers, the number is not ...

Answered by anindyaadhikari13
2

Required Answer:-

Question:

  • Write a program to check whether square root of a given number is prime or not.

Solution:

Here is the code.

  1. import java.util.*;
  2. public class JavaBrainly {
  3. public static void main(String[] args) {
  4. int n;
  5. Scanner sc=new Scanner(System.in);
  6. System.out.print("Enter the number: ");
  7. n=sc.nextInt();
  8. double sqrt=Math.sqrt(n);
  9. if(sqrt!=(int)sqrt)
  10. {
  11. System.out.println("Square root of the number is not prime.");
  12. }
  13. else
  14. {
  15. int c=0;
  16. for(int i=1;i<=(int)sqrt;i++)
  17. {
  18. if((int)sqrt%i==0)
  19. c++;
  20. }
  21. if(c==2)
  22. System.out.println("Square root of the number is prime.");
  23. else
  24. System.out.println("Square root of the number is not prime.");
  25. }
  26. sc.close();
  27. }
  28. }

Explanation:

  • We will take the number as input and then, we will find out the square root of the number. Note that square root of a number can be irrational. In that case, it will not be a prime number. A number is said to be a prime number if it's divisible by 1 and itself. After calculating the square root, we will check if the resultant number is a prime number or not.

Output is attached for verification.

Attachments:
Similar questions