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 does the q{ } operator do?
package MYCALC; use Exporter; our @EXPORT = (); our @ISA = qw(Exporter); our @EXPORT_OK = qw(addition multi); our %EXPORT_TAGS = (DEFAULT => [qw(&addition)],Both => [qw(&addition & +multi)]); sub addition { return $_[0] + $_[1]; } sub multi { return $_[0] * $_[1]; } 1; Program: use strict; use warnings; my @list = qw (2 2); use Module qw(:DEFAULT); print addition(@list),"\n"; Above coding is my module MYCALC and the program which using this module, I have not exported any function using @EXPORT, but I have used the DEFAULT in %EXPORT_TAGS with the function addition, when I call this function from the main it says the error as,
How to dereference a reference?
How to do comment in perl?
Write a program that shows the distinction between child and parent process?
What are the different string manipulation operators in perl?
You want to open and read data files with perl. How would you do that?
Explain '->' in perl?
Define print() function in perl?
Give an example of using the -n and -p option.
Explain returning values from subroutines?
What is it meants by '$_'?
What does Perl do if you try to exploit the execve(2) race involving setuid scripts?
Which has the highest precedence, List or Terms? Explain?
Write a program to concatenate the $firststring and $secondstring and result of these strings should be separated by a single space.