Computer Science, asked by ankit584756, 1 year ago

write a program in c++ to print the prime numbers between 100to200

Answers

Answered by QGP
0

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  \sf n is prime or not, we check divisibility of the number by each number from 2 till \sf \sqrt{n}.

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;

}



Attachments:
Similar questions