Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Prime Number Checker

/*
	C++ Prime Number Checker program by lolman_ks.
    This logic can be used to build this program in other languages also.
*/
/*
	Logic: Divide the number by all numbers less than half of the number to
    check. If it is divisible by any of them, return False.
*/

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int number){
	if(number == 1) return false; //1 is neither prime nor composite.
  	else if(number < 1) return false;
  	/*
    	O is the 0th multiple of every number, x * 0 = 0. Therefore 0 is not
        prime.
        Any -ve number is also composite because it has at least 3 factors:
        Itself, 1 and -1.
    */
  	else if(number == 2) return true; //Only even prime number;
  
  	else{
    	for(int i = 0;i <= ceil(number / 2);++i){
        	if(number % i == 0) return false; //Check exact divisibility.
        }
      	return true; 
      	/*
        	Return true if the loop is not broken and the number is not exactly
            divisible by any number.
        */
    }
}

//Implementations of this function.
//Check prime numbers upto a limit.

int main(){
	int limit;
  	cout << "Enter limit: ";cin >> limit;
  
  	for(int i = 0;i <= limit;++i){
		cout << i << ": " << isPrime(i) << endl;
	}
}

/*
	I hope my answers are useful to you, please promote them if they are.
    #lolman_ks.
*/
Comment

prime number checker

num = int(input('enter a number : '))

while num%2 and num%3 and num%5 ==0:
    print( "it is not a prime nunber")
    break
else:
    
    print( "it is a prime nunber")
Comment

PREVIOUS NEXT
Code Example
Cpp :: fast input output cpp 
Cpp :: no indentation latex 
Cpp :: sfml local mouse position 
Cpp :: c++ hide console 
Cpp :: excel vba delete worksheet if exists 
Cpp :: c++ alphabet array 
Cpp :: flutter datetime format 
Cpp :: separation between paragraphs latex 
Cpp :: c++ lambda thread example 
Cpp :: avrational compare 
Cpp :: c++ sleep for seconds 
Cpp :: unordered_map of pair and int 
Cpp :: output coloured text in cpp 
Cpp :: cpp read csv 
Cpp :: how to append one vector to another c++ 
Cpp :: how to iterate in string in c++ 
Cpp :: unreal get eobjecttypequery cpp´ 
Cpp :: get ascii value of qchar 
Cpp :: unknown type name pid_t 
Cpp :: c++ vector combine two vectors 
Cpp :: c++ cmd program run in background 
Cpp :: c++ enum rand 
Cpp :: c++ std::find with lambda 
Cpp :: check if c++ is installed 
Cpp :: c++ vector add only unique elements 
Cpp :: rank() in c++ 
Cpp :: c++ create threads 
Cpp :: c++ user input 
Cpp :: infinite loop c++ 
Cpp :: c++ swapping two numbers 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =