Java Interface | Practice | GeeksforGeeks

Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body).  
Given an inteface in1 which includes a method display which takes an integer as input .
interface in1
{
    void display(int p);
}
Task is to write a class testClass which implements interface in1 and has a method named display which takes an integer as input and total number of prime numbers between 2 and integer k (including it). Main function in written for you in the editor.
Input: 
The first line will contain an integer T (number of test cases). Each test case consists of an integer n.
Output: 
Print total number of prime numbers between 2 and the given number ( including them ).
Constraints: 
1 < = T < = 1000
2<= n <= 104
Example:
Input:
2
13
19
Output:
6
8
0
nikhilkalkalbin 9 hours
i think my code is right way  to solve this problem    here is a APPROCH TO  solve this not execetly
 
==============================
import java.util.*;class HelloWorld {   public static void main(String[] args) {       Scanner sc=new Scanner(System.in);       int a=sc.nextInt();       int count=0;       for(int i=2;i<a;i++){           if(checkprime(i)){               count++;           }       }System.out.print(count);   }   public static boolean checkprime(int a){       int t=a/2;       for(int i=2;i<t;i++){           if(a%i==0){               return false;           }       }return true;   }}
0
sknurhassan041 week ago
class testClass implements in1{   boolean isPrime(int num)       {           for(int i = 2; i<=Math.sqrt(num); i++)           {               if(num%i==0)               {                   return false;               }           }                      return true;       }                 public void display(int k)   {          // Add your code here.              int count =0;              for(int n =2; n<=k;n++)       {           if(isPrime(n))           {               count++;           }       }              System.out.println(count);          }}
+1
mdsaif04052 months ago
    boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
    public void display(int n)
    {   
        int prime=0;
        for (int i = 2; i <= n; i++) {
            if( isPrime(i) ){
                prime++;
            }
        }
        System.out.println(prime);  
    }
+2
badgujarsachin834 months ago
class testClass implements in1
{
    boolean isprime(int n){
        if(n==1||n==0){
            return false;
        }
        for(int i=2;i<=Math.sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    public void display(int k)
    {   
        // Add your code here.
        int count=0;
        for(int i=2;i<=k;i++){
            if(isprime(i)){
                count++;
            }
        }
        System.out.println(count);
    }
}
0
ramyakovi99
This comment was deleted.
-1
Samyak Jain10 months ago
Samyak Jain
class testClass implements in1{
    int sum=0;    int x,i,j;
    public void display(int k)    {         for(x=2;x<=k;x++){           //for numbers between 2 and 13            for(i=2;i<x;i++){ for="" dividing="" to="" check="" the="" number="" is="" prime="" if(x%i="=0){" break;="" }="" }="" if(i="=x){" sum++;="" }="" }="" system.out.println(sum);="" add="" your="" code="" here.="" }="" }="">
-1
Shashank Agarwal2 years ago
Shashank Agarwal
class testClass implements in1{    public void display(int k)    {           // Add your code here.        int flag=1;        int count=0;        for(int i=3;i<=k;i=i+2)        {            for(int j=2;j<=i/2;j++)            {                if(i%j==0)                {                    flag=0;                    break;                }                else{                    flag=1;                }            }            if(flag==1)            {                count++;            }        }       count=count+1;        System.out.println(count);    }}best solution
-1
Krishna Pavan2 years ago
Krishna Pavan
Took just 1.10 seconds.
public void display(int k)    {           // Add your code here.        int total = 1;        int count = 0;        for(int i = 2; i <= k; i++)        {            for(int j = 2; j < i; j++)            {                if(i % j == 0)                {                    count = 0;                    break;                }                else                {                    count = 1;                }            }            if(count == 1)            {                total++;            }        }              System.out.println(total);    }
-1
Shreyash Atmaram Yewale2 years ago
Shreyash Atmaram Yewale
Best Solution for the problem.// Add your code here.        boolean isPrime=true;        int cnt=0;        int i=2;        while(i<=k)        {            for(int j=2;j<=i/2;j++)            {                if(i%j==0)                {                    isPrime=false;                    cnt++;                    break;                }            }            i++;
        }        int arr[]=new int[k-1];        int p=0;        for(int c=2;c<=k;c++)        {            arr[p]=c;            p++;        }
        int ans=arr.length-cnt++;        System.out.println(ans);    }
-1
Saransh2 years ago
Saransh
class testClass implements in1{    boolean isPrime(int n) {        for(int i = 2; i <= Math.sqrt(n); i++) {            if(n%i == 0)                return false;        }        return true;    }     public void display(int k)    {           int count = 0;         for(int i = 2; i <= k; i++) {            if(isPrime(i))                count++;        }         System.out.println(count);    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.