A prime number is any positive integer that is divisible by itself and the number 1.
Write down a c++ program to display all prime numbers less than 50
Answers
Primes till a Limit - C++
Prime Numbers are those which are divisible by only 1 and itself.
For example, 7 is a Prime, because it is divisible only by 1 and 7.
8 is not a Prime, because it is divisible by 2 and 4 also.
So, to check if a number is Prime, we must check if it is divisible by the numbers between 1 and itself.
We can run a loop for this. However, we do not need to check divisibility from 2 to number all the way.
We only need to check divisibility from 2 to sqrt(number) only. We don't need to go beyond square root of number.
If we can check that the number is not divisible by any number in this range, we can be sure that it is a Prime.
Here's a C++ code which implements a Function to check if a number is Prime or not. It returns a bool value: true or false depending on whether the number is Prime or Not, respectively.
Then, we run a loop from 2 to 50, and print all the primes.
The variable limit can be changed to print more or less primes as required.
#include <iostream>
#include <cmath> //Including for sqrt() function
using namespace std;
bool isPrime(int num) //Function to check if a given number is Prime
{
//We check divisibility of num by all numbers from 2 to sqrt(num)
//If divisibility is found, number is not Prime, so return false
for(int i=2;i<=(int)sqrt(num);i++)
{
if(num % i == 0)
{
return false;
}
}
//If no divisibility is found, return true
return true;
}
int main()
{
int limit = 50; //Setting Limit to 50
cout << "Primes less than " << limit << " are:" << endl;
for(int i=2;i<limit;i++)
{
if(isPrime(i)) //Calling Prime Checking Function
{
cout << i << endl; //Printing Primes
}
}
return 0;
}
Answer
The following C++ program prints the prime number that is less than 50.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{ cout << "Prime Numbers less than 50:\n";h
for(int i=2;i<=50;++i) //loop to check for each number in the range
{ int ctr=0; //to maintain factor count
for(int j=2;j<=sqrt(i);++j) //checking for factors
{ if(i%j==0)
ctr=1; //increasing factor count when found
}
if(ctr==0) //checking and printing prime numbers
cout<<i<<" ";
}
return 0;
}
Explanation:
Prime Number
A prime number is that positive integer that cannot be divided with any other number except 1 and itself.
Like 2,3,5,7 etc.
Let's understand the code written above:
- We need to use cmath library in the code so that we can use the sqrt function to compute the square root.
- Then we write two loops. The first outer loop starts with 2, since 1 is not a prime number and runs till 50. And the inner loop starts with 2 and runs to the square root of the i.
- If at any point i is divided by j that means i is the multiple of j. That violates the condition of a prime number.
- So we maintain a cnt variable to keep track of whether the number is divisible by any other number.
- If the number cannot be divided by any other number then we print the number as the prime number.
#SPJ2