write a program in c++ to print the prime numbers between 100to200
Answers
Prime Number Program in C++
Prime Numbers are those which have exactly two factors, 1 and the number itself.
A few examples of primes include 2, 3, 5, 7, 23, 101, etc.
To check if a number is prime or not, we check divisibility of the number by each number from 2 till
.
If the number is found to be divisible anywhere, then it is not a prime. Otherwise it is a prime (since no divisibility is found).
Here is a C++ program to print prime numbers in a given range:
/* Program to print primes in a given range */
#include <iostream>
#include <cmath> //Importing <cmath> for sqrt() function
using namespace std;
bool isPrime(int n) //Function to check if an integer n is prime or not
{
for(int i=2;i<=(int)sqrt(n);i++) //We check divisibility of n by each number from 2 to sqrt(n)
{
if(n%i==0) //If divisibility is found, then n is not prime and so false is returned
return false;
}
return true; //If no divisibility found, then function returns true
}
int main()
{
int lowerLimit = 100; //Lower Limit
int upperLimit = 200; //Upper Limit
for(int i=lowerLimit;i<=upperLimit;i++) //Running a loop in the given range
{
if(isPrime(i)) //If the number is prime, then it is printed
{
cout << i << endl;
}
}
return 0;
}
![](https://hi-static.z-dn.net/files/d21/80e21f71ab2911e91ad07c9fcef8e3d3.jpg)
![](https://hi-static.z-dn.net/files/d1f/8481c51b40beaf659b945dd24757aeb6.jpg)