W.a.p in Java to create an interface prime and declare final variable and calculate all prime no between them.
Answers
Answer:
import java.util.scanner;
class Primenumber2
{
public static void main(String[]args)
{
Scanner scanner = new Scanner(System.in);
int i=0;
int num=0;
String primenumber=" ";
sysytem.out.println("Enter the value of n:")
int n =scanner.nextInt();
scanner.close();
for(i=1;i<=n;i++)
{
if(i%num==0)
{
counter +=1;
}
}
if (counter==2)
{
primenumber = primenumber+i+" " ;
}
}
System.out.println("Prime number from 1 to n are:")
System.out.println(primenumber);
}
}
Explanation: here first of all we will import java module and then define the class primenumbers2 then we will take a string argument and then we will take a integer i and num and assign them values 0 and after it we will start a for loop from 1 to n numbers in this loop we will check the counter values and then we will start another loop that if i divided by num==0 and in this the value of the counter will be incremented and we can now print the prime numbers.