Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer.

Program Code in JAVA:
import java.io.*;
class Primenos
{
public static void main(String[] args) throws Exception
{
int n,fact;
System.out.println("Enter the range of Prime number you 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");
}
}
}

Program Output:
c:\java>javac Primenos.java
c:\java>java Primenos
Enter the range of Prime number you want 50
Prime No's from 1 to 50 is
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: