Functions- prime
Problem Statement
Given two integers N and M, your task is to print the number of primes present between N and M (both included).
Note:- You have already provided a function that will check if the given number is prime or not. To use the given function you need to call check_prime(x) where x is the number you want to check. If the given number is prime the function will return 1 else it returns 0.
Note:- You have already provided a function that will check if the given number is prime or not. To use the given function you need to call check_prime(x) where x is the number you want to check. If the given number is prime the function will return 1 else it returns 0.
c++ code:
#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;
int checkPrimeNumber(int n) {
int j, flag = 1;
for (j = 2; j <= n / 2; ++j) {
if (n % j == 0) {
flag = 0;
break;
}
}
return flag;
}
int main() {
int n1, n2, i, flag;
scanf("%d %d", &n1, &n2);
if (n1 > n2) {
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
}
int ans=0;
for (i = n1 + 1; i < n2; ++i) {
flag = checkPrimeNumber(i);
if (flag == 1) {
ans++;
}
}
cout<<ans<<endl;
return 0;
}