write a script to generate n prime no.s?

Answers were Sorted based on User's Feedback



write a script to generate n prime no.s?..

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

write a script to generate n prime no.s?..

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

Post New Answer

More CGI Perl Interview Questions

What is the difference between use and require in perl programming?

0 Answers  


You want to concatenate strings with perl. How would you do that?

0 Answers  


I have created a CGI-based page,after entering all the values in to the fields, How to get the output on the web browser using Perl

4 Answers  


What is the difference between use and require in perl?

0 Answers  


What is cpan ? What are the modules coming under this?

0 Answers  






Is perl compiler or interpreter?

0 Answers  


write a script to check whether user enter a value is a leap year or not?

3 Answers   Oracle, Persistent, ViPrak,


What does this symbol mean '->'?

0 Answers  


Perl regular expressions are greedy" what does this mean?

0 Answers  


explain the various functions/directives in perl that allow you to include/import a module. Also, state the differences between them.

0 Answers  


What are hashes?

0 Answers  


Explain regular expression in perl?

0 Answers  


Categories