Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer.
Program Code:
import java.io.*;
class Primenos
{
public static void main(String[] args) throws Exception
{
int n,fact;
System.out.println("Enter the range of Prime No U want");
DataInputStream dis=new DataInputStream(System.in);
n=Integer.parseInt(dis.readLine());
System.out.println("Prime No's from 1 to "+n+" is");
for(int i=1;i<=n;i++)
{
fact=1;
for(int x=1;x<i;x++)
if(i%x==0)
fact++;
if(fact==2)
System.out.print(i+"\t");
}
}
}
Post A Comment:
0 comments: