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
What are prefix dereferencer? List them.
Define say() function in perl?
Which operator in perl is used for the concatenation of two strings?
What do you mean by context of a subroutine?
What does this symbol mean '->'?
Why Perl aliases are considered to be faster than references?
Explain use of ‘my’ keyword in perl?
Explain the internal working of cgi
you are required to replace a char in a string and store the number of replacements. How would you do that?
Which functions in Perl allows you to include a module file or a module and what is the difference between them?
Explain about the applications of perl?
Explain gmtime() function in perl?
What are the features of perl language?
What exactly is grooving and shortening of the array?
Write a program to decode the data in the form using cgi programming