Explanation of the method for determining prime numbers (cyclic structure java) in C Language experiments

Source: Internet
Author: User

Explanation of the method for determining prime numbers (cyclic structure java) in C Language experiments
Explanation of Problem Description using the method of determining prime numbers (cyclic structure java) in C Language experiments

Enter any positive integer on the keyboard and determine whether the number is a prime number.

If it is a prime number, the output "This is a prime ."

Otherwise, the output "This is not a prime ."

Input

Enter any positive integer n (1 <=n <= 1000000 ).

Output

Determine whether n is a prime number and output the result:

If n is a prime number, the output is "This is a prime ."

Otherwise, the output "This is not a prime ."

Sample Input

3

Sample Output


 


This is a prime.


AC code:


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n;
        n = in.nextInt();
        int i,f=1;
        if (n == 1)
            System.out.println("This is not a prime.");
        else if(n==2)
            System.out.println("This is a prime.");
        else {
          for(i=2;i<n;i++){
             if(n%i==0){
               f = 0;
             }
          }
          if(f == 0)System.out.println("This is not a prime.");
          else System.out.println("This is a prime.");
        }
    }
}


The result of the code is the same but the result of OLE (timeout):


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n;
        n = in.nextInt();
        int i;
        if (n == 1)
            System.out.println("This is not a prime.");
        else if(n==2)
            System.out.println("This is a prime.");
        else {
          for(i=2;i<n;i++){
             if(n%i==0){
               System.out.println("This is not a prime.");
               break;
             }
            else{
                System.out.println("This is a prime.");
            }
          }
        }
    }
}

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.