Google Search

Monday, October 10, 2011

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


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


import java.lang.*;
import java.io.*;
class prime
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
for(int r=0;r<n;r++)
{
int t=0;
for(int i=1;i<r;i++)
{
if (r%i==0)
{
t++;
}
}
if(t<2)
{
System.out.print(r);
System.out.print(" ");
}
}
}
}

0 comments:

Post a Comment