write a script to generate n prime no.s?
Answers were Sorted based on User's Feedback
Answer / venkat
#!c:\perl\bin\perl
$Count = 0;
$pt = 2;
while ( $Count < @ARGV[0] )
{
if (isPrimary($pt))
{
print "$pt\n";
$Count++;
}
$pt++;
}
sub isPrimary
{
$flag = 1;
for ($i=2; $i<=$_[0]/2; $i++)
{
if ($_[0] % $i == 0)
{
$flag = 0;
}
}
return $flag;
}
| Is This Answer Correct ? | 16 Yes | 4 No |
Answer / 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 |
Mention the difference between die and exit in Perl?
What are perl array functions?
Which functions in Perl allows you to include a module file or a module and what is the difference between them?
What does perl do in linux?
How to dereference a reference?
What is 'commit' command in perl?
Explain USE and REQUIREMENT statements?
How would you ensure the re-use and maximum readability of your perl code?
What happens when you return a reference to a private variable?
Where the command line arguments are stored and if you want to read command-line arguments with Perl, how would you do that?
What arguments do you frequently use for the Perl interpreter and what do they mean?
What is the use of 'ne' operator?