write a script to generate n prime no.s?
Answer Posted / dushant
import java.io.*;
import java.util.*;
class Prime
{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int cnt = 0,j;
Prime()
{
try
{
System.out.print("Enter A Number :");
int n = Integer.parseInt(br.readLine());
for(int i =1 ;i <= n ;i++)
{
for( j = 1 ; j <= i ;j++)
{
if(j==1) //as 1 is Common Factor For
All
{
cnt ++;
}
if(i % j == 0 && j !=i) //Condition j!=i because at
1st loop
//i=1 j=1 so i%j==0 and 1 is not prime no
{
cnt ++;
}
if(cnt == 3)
{
break;
}
if(cnt==2 && i == j )
{
System.out.println(i);
}
}
cnt=0;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
new Prime();
}
}
| Is This Answer Correct ? | 7 Yes | 3 No |
Post New Answer View All Answers
Explain about typeglobs?
Create a function that is only available inside the scope where it is defined ?
What does the qx{ } operator do?
Hi, I am a accountant. I am preparing a balance sheet but because of staff shortage and time pressures I cant complete it on time. There is lot of common data with last years which I need not retype and I can manage by editing last year’s balance sheet ? Is their any software on net where I can do this easily??
Explain regular expression in perl?
How many data types are there in perl?
What happens in dereferencing?
Why do you use Perl?
You want to read command-line arguements with perl. How would you do that?
Where do we require ‘chomp’ and what does it mean?
Explain the internal working of cgi
What is an interpolation in perl?
How can I implement the function overloading in Perl ? I read about the operator overloading, I do not know how to implement the function overloading. Thanks in advance ?
How to turn on Perl warnings? Why is that important?
Explain perl.