Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

prime numbers

#include <iostream>
#include <cmath>
#include <iostream>

#define el "
"

using namespace std;

bool isprime(int n)
{
    if (n <= 1)
    {
        return 0;
    }
 
    for(int i=2 ; i <= sqrt(n); i++) // [sqrt(n)] is faster in calculations than [n/2]
    {
        if(n%i == 0)
        {
            return 0;
        }
    }
    return 1;
};

int main() 
{
    long long num;

    cin >> num; // ~~~ Enter a number
 
    (isprime(num))? cout << "The number (" << num <<") is a prime" << el : cout << "The number (" << num <<") isn't a prime" << el;
        
    return 0;
}
Source by code.sololearn.com #
 
PREVIOUS NEXT
Tagged: #prime #numbers
ADD COMMENT
Topic
Name
4+8 =