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


Please Help Members By Posting Answers For Below Questions

How to create a directory in perl?

792


Write a cgi program to show the header part?

722


What does the q{ } operator do?

688


“Perl regular expressions match the longest string possible”. What is the name of this match?

756


What is the difference between die and exit in perl?

771


How to renaming a file in perl programming?

765


How to replace perl array elements?

720


Explain ivalue in perl?

744


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,

2489


what are the strategies followed for multiple form interaction in cgi programs?

691


What are the arguements we normally use for perl interpreter?

746


Why do we use "use strict" in perl?

749


how to extract pin_code,phone_number,year from text file using regular expressions in perl

2049


How do you set environment variables in perl?

770


There are some duplicate entries in an array and you want to remove them. How would you do that?

725