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
How do I read command-line arguments with Perl?
How many types of operators are used in the Perl?
How do I replace every TAB character in a file with a comma?
Explain socket programming in perl?
Explain the difference between "my" and "local" variable scope declarations. ?
Give an example of using the -n and -p option.
How can I display all array element in which each element will display on next line in perl ?
What are the different string manipulation operators in perl?
What are perl strings?
Show the use of sockets for the server and client side of a conversation?
What is subroutine in perl?
What does file test operators do in perl?
What is boolean context?
How to concatenate strings with perl?
What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), $ap ]; }' ?