Showing posts with label Functions- prime. Show all posts
Showing posts with label Functions- prime. Show all posts

Saturday, July 2, 2022

Functions- prime

 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.

c++ code:
#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;

int checkPrimeNumber(int n) {
  int j, flag = 1;

  for (= 2; j <= n / 2; ++j) {

    if (% 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 (= n1 + 1; i < n2; ++i) {
    flag = checkPrimeNumber(i);

    if (flag == 1) {
      ans++;
    }
  }

  cout<<ans<<endl;
  
  return 0;
}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...