Question

int is_prime(int n) { if (n <= 1) return 0; if (n % 2 == 0...

int is_prime(int n)
{
if (n <= 1) return 0;
if (n % 2 == 0 && n > 2) return 0;
for (int i = 3; i < n / 2; i += 2)
if (n % i == 0)
return 0;

return 1;
}

What do the three lines in this function mean?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.io.*;
import java.util.Scanner;
public class TestPrime { // Here taking class name as TestPrime
public int is_prime(int n) { // Definition of the method is_prime()
/*if (n <= 1)
return 0; //The above condition is true then returning 0 to the main()
if (n % 2 == 0 && n > 2)
return 0; //The above condition is true then returning 0 to the main() */
for (int i = 3; i < n / 2; i += 2)
if (n % i == 0) //Here checking the condition if remainder is zero
return 0; //The above condition is true then returning 0 to the main()
return 1;
}
public static void main(String args[]) // Start of main()
{
int m=0; // Here initializing the value of integer varaible called yr
TestPrime ob = new TestPrime(); // Here creating the object for class TestPrime
Scanner in = new Scanner(System.in);
System.out.println("Enter Value of n: "); // Here taking Input from the User
m = in.nextInt(); // Here scanning the input
System.out.println(ob.is_prime(m)); // Here calling the leapyear() method
}// End of main()
} // End of class TestPrime

Output:

Add a comment
Know the answer?
Add Answer to:
int is_prime(int n) { if (n <= 1) return 0; if (n % 2 == 0...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT