Answers for "find prime numbers in a range"

23

prime numbers

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ...
Posted by: Guest on January-07-2021
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 "find prime numbers in a range"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language