Allow the user to specify the upper limit. Either allow the user to specify the
ID: 3565468 • Letter: A
Question
Allow the user to specify the upper limit.
Either allow the user to specify the number of trials, or define a (reasonable) CONSTANT in the program.
Use this design to help make the program:
Write a program that compares the theoretical probability of choosing a prime number within a specified range with the experimental probability of choosing a prime number in the same range. Experimental probability is determined by performing an appropriate number of trials! Allow the user to specify the upper limit. Either allow the user to specify the number of trials, or define a (reasonable) CONSTANT in the program. Use this design to help make the program:Explanation / Answer
import java.util.Scanner;
//perfectly working
public class NPrimes
{
public static void main(String[] args)
{
System.out.println("enter no. of primes want:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
//int n1=sc.nextInt();
int p=3;
int c=1;
int i,f=1;
if(n>0)
{
System.out.println("2");
}
while(c<n)
{
i=2;f=1;
while(f>0)
{
if(p%i!=0)
{
i++;
if(i==p)
{
System.out.println(p);
f=0;
c++;
p++;
}
else
{
f=1;
continue;
}
}
else
{
p++;
f=0;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.