Tuesday, 12 March 2013

Java Program To Print Prime Numbers Using Command-Line Arguments


/* Java Program That Promotes The User For An Integer And Then Prints Out
All Prime Numbers upto That integer */


// File Name: prime.java

import java.io.*;
class prime {
public static void main(String[] args) throws IOException {
int n,i,j;
n=Integer.parseInt(args[0]);
System.out.println("Given Value Of n Is :"+n);
System.out.print("Prime numbers Generated For "+n+" Are :");
for(i=0;i<n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
break;
}
if(i==j)
System.out.print(i+",");
}
System.out.print("\n");
System.out.println("www.shashanktechnologies.blogspot.com");
}
}


OUTPUT:

COMPILATION: G:\shashank>javac prime.java

INTERPRETATION: G:\shashank>java prime 25



No comments:

Post a Comment