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
  • C language int is_prime(int n) { if (n <= 1) return 0; if (n % 2...

    C language 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?

  • 1. public int function(int x, int n) { if (n == 0) return 1; return x...

    1. public int function(int x, int n) { if (n == 0) return 1; return x * function(x, n -1); } function(3,3) - What is the expected output? 3 12 9 27 2. int fun(int x) { if(x == 0) return 1; else return fun(x - 1); } fun(4) 18 1 24 4 3. Which one of the following calls results 6? int mystery(int n){ if (n == 1) return 1; else return n * mystery(n - 1); } mystery(3)...

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...

  • What is the base case? -------------------------------------------------- int fibo(int n) {     if(n <= 2){         return...

    What is the base case? -------------------------------------------------- int fibo(int n) {     if(n <= 2){         return n-1;     }     else{         return fibo(n-1) + fibo(n-2);     } } --------------------------------------------------------------------------- int getFibo(int n){     if(n<=2){         return n-1;     }     else{         int term[n+1], i;         term[0] = 0, term[1] = 1;         for(i = 2;i=             term[i] = term[i-1]+term[i-2];         }         return term[n];     } }

  • How to prove G(n)=n+1 in this algorithm? 1. if (n 0) 2. return 1 3. else if (n1) f 4. return 2 5. else if (n 2) 6. return 3 7. else if (n3) t 8. return 4 else f 9. int OGnew int[n 11 10. G[O]1 1...

    How to prove G(n)=n+1 in this algorithm? 1. if (n 0) 2. return 1 3. else if (n1) f 4. return 2 5. else if (n 2) 6. return 3 7. else if (n3) t 8. return 4 else f 9. int OGnew int[n 11 10. G[O]1 12. G[2]3 13. G[3]4 14. int i:-4 15. while (i<n) t 16. if (i mod 20) else ( 20. return G[n] 1. if (n 0) 2. return 1 3. else if (n1) f...

  • Int h(int n) { if (n == 0) return 0; else return h(n/10) + n%10;} What...

    Int h(int n) { if (n == 0) return 0; else return h(n/10) + n%10;} What is the output for each function call (a) Function call h(736).   Output: (b) Function call h(12345).   Output: Answer options: 16,15 26,25 36,35 46,45

  • #include <stdio.h>    int josephus(int n, int k) {   if (n == 1)     return 1;   else...

    #include <stdio.h>    int josephus(int n, int k) {   if (n == 1)     return 1;   else     /* The position returned by josephus(n - 1, k) is adjusted because the        recursive call josephus(n - 1, k) considers the original position         k%n + 1 as position 1 */     return (josephus(n - 1, k) + k-1) % n + 1; }    // Driver Program to test above function int main() {   int n = 14;   int k = 2;   printf("The chosen place...

  • The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int...

    The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int n) { int d; for (d = 2; d < n; d++) { if (!(n % d)) return 0; } return 1; } int main() { if (is_prime(7)) printf("Seven is Prime!\n");    return 0; } It is unnecessary to divide n by all numbers between 2 and n - 1 to determine if n is prime. Only divisors up to and including  need to be...

  • b) Consider the following code. public static int f(int n) if (n == 1) return 0;...

    b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...

  • Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int...

    Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int res2 = foo(N-2); return res1-res2; } a) (6 points) Write the recurrence formula for the time complexity of this function (including the base cases) for N>=0. You do NOT need to solve it. b) (5 points) Draw the tree that shows the function calls performed in order to compute foo(8) (the root will be foo(8) and it will have a child...

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