Determines whether a number is a prime number.

Source: Internet
Author: User
Tags readline square root

To determine the prime number, you can open the given number and use this value to divide the number from 1 to open the house or only the largest integer smaller than the given one. If the number cannot be divisible, this is a prime number, otherwise, it is not a prime number.

Next, create a console application, enter the number to be judged in the console, use the int type variable record, and then determine whether the input number is a prime number. The prompt message is displayed. The code is as follows:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace JudgePNum
{
Class Program
    {
Static void Main (string [] args)
        {
Console. WriteLine ("Enter the number to be judged:"); // the system prompts you to enter the information.
Int j = 0; // defines an int variable to record the maximum number after the square root of the input number.
Int intNum = Convert. ToInt32 (Console. ReadLine (); // records the number of inputs;
J = (int) Math. Ceiling (Math. Sqrt (Convert. ToDouble (intNum); // assign a value to j,
// CEILING (number, significance), rounds up the parameter Number (in the direction of increasing the absolute value) to a multiple of the nearest significance
// Sqrt (number) returns the square root of the value.
Int intFlag = 0;
For (int I = 2; I <j; I ++)
            {
IntFlag + = Convert. ToInt32 (Math. IEEERemainder (intNum. I); // returns and records
            }
If (intFlag = 0) // Determine whether the remainder is 0
Console. WriteLine (intNum + "is not a prime number. ");
Else
Console. WriteLine (intNum + "is a prime number. ");
Console. ReadLine ();
           
        }
    }
}

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.