Jump to content

IsPrime Function Explanation needed

Go to solution Solved by mansoor_,

bool is a type of data (boolean) that takes the values true or false.

(hence the syntax  [bool Varname] : defines a boolean variable Varname)

In this code the variable primeflag, is a boolean variable,

The initial batch of code checks whether the number (k, which the loop is currently on),

is a prime number [if (number % i == 0)]

if so, then primeflag will be true, and the number (k) will be printed.

#include <iostream>#include <iomanip>#include <sstream>#include<stdio.h>#include<conio.h>#include <string>#include <fstream>#include <cmath>using namespace std;bool isPrime(int number){    if (number == 1)        return false;	bool primeflage = true;	int i;	for (i = 2; i <= sqrt(number) ; i++)	{		if (number % i == 0)		{			primeflage = false;			break;		}	}	return primeflage;}int main(){	int k,a,b;	cout << "Enter the start rang: " << "\n";	cin >> a;	cout << "\n";	cout << "Enter the end rang: " << "\n";	cin >> b;	for (int k = a; k < b; k++)	{		if (isPrime(k) == true)			cout << k << "\t";	}	cout << "\n";	return 0;}

I don't get it at all can someone explain it for me by some easy way ?

Link to comment
https://linustechtips.com/topic/285567-isprime-function-explanation-needed/
Share on other sites

Link to post
Share on other sites

bool is a type of data (boolean) that takes the values true or false.

(hence the syntax  [bool Varname] : defines a boolean variable Varname)

In this code the variable primeflag, is a boolean variable,

The initial batch of code checks whether the number (k, which the loop is currently on),

is a prime number [if (number % i == 0)]

if so, then primeflag will be true, and the number (k) will be printed.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×