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 to check the status of airplane mode (enable/disable) in perl for Android mobile?
What does `$result = f() .. g()' really return?
Why should I use the -w argument with my Perl programs?
Demonstrate subroutines in perl with a simple example.
What is the use of -w, -t and strict in Perl?
What are the functions that can be performed using cgi program?
How to deleting an existing file in perl programming?
Explain split function in perl?
Explain the functioning of conditional structures in perl.
What is the use of '>>' in perl?
What are the different types of perl operators?
What are the various file operations in perl. Explain with example.
Explain '->' in perl?
How many data types are there in perl?
If you want to empty an array then how would you do that?