Saturday, July 2, 2022

Number of Primes - Easy Version

 Number of Primes - Easy Version

Problem Statement
You are given a number n. Your task is to print the number of prime numbers before that number.

#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
    if (<= 1)
        return false;


    for (int i = 2; i < n; i++)
        if (% i == 0)
            return false;

    return true;
}
int ppp=0;
void printPrime(int n)
{
    for (int i = 2; i <= n; i++) {
        if (isPrime(i))
            ppp++;
    }
    cout<<ppp<<endl;
    ppp=0;
}

int main()
{
    int t;
    cin>>t;

    while(t--){
    int n;cin>>n;
    printPrime(n);
    }
}

No comments:

Post a Comment

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 >    ...