Answers for "prime number in a range"

C++
0

how to find prime numbers in a range

class main{
   public static void main(String [] args){
      Scanner sc = new Scanner(System.in); // Scanner function should be imported//
        int a = sc.nextInt();
        int c = 0;
         for(int i=2;i<=a;i++) {
    	      
    	    	 for(int j=2;j<=i;j++) {
                 if(i==j) {  
                      c++;
    	    	   }
                   
    	         if(i%j==0) {  
    	    	   break;
    	    	 }  
    	    	 }
                 }
    	    System.out.println("\nTotal prime numbers ("+a+") - "+c);
  }
}
Posted by: Guest on June-30-2021
-1

prime number in a range

//Prime Numbers & Sieve of Eratosthenes
#include <iostream>
#include<math.h>

using namespace std;
int count_prime(int n)
{
    bool bo[n+1];
    bo[0]=false;
    bo[1]=false;
    for(int i=2;i<=n;i++)
    {
        bo[i]=true;
    }
    int count=0;
    for(int i=2;i<=sqrt(n);i++)
    {
        for(int j=2*i;j<=n;j=j+i)
        {
            bo[j]=false;
        }
    }
    for(int i=2;i<=n;i++)
    {
        if(bo[i]==true)
        {
            count++;
        }
    }
    return count;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int ans=count_prime(n);
        cout<<ans<<endl;
    }
    return 0;
}
Posted by: Guest on June-08-2021

Code answers related to "prime number in a range"

Browse Popular Code Answers by Language