Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 check the status of airplane mode (enable/disable) in perl for Android mobile?

3086


What does `$result = f() .. g()' really return?

1197


Why should I use the -w argument with my Perl programs?

960


Demonstrate subroutines in perl with a simple example.

857


What is the use of -w, -t and strict in Perl?

988


What are the functions that can be performed using cgi program?

932


How to deleting an existing file in perl programming?

905


Explain split function in perl?

995


Explain the functioning of conditional structures in perl.

945


What is the use of '>>' in perl?

1006


What are the different types of perl operators?

1075


What are the various file operations in perl. Explain with example.

947


Explain '->' in perl?

927


How many data types are there in perl?

962


If you want to empty an array then how would you do that?

1098