This program explain you how to check the number is prime or not if user input the value from keyboard. Here is the source code :
import java.io.*;
class PrimeNumber {
public static void main(String[] args) throws Exception{
int i;
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number to Check:");
int num = Integer.parseInt(bf.readLine());
System.out.println("Prime number: ");
for (i=1; i < num; i++ ){
int j;
for (j=2; j<i; j++){
int n = i%j;
if (n==0){
break;
}
}
if(i == j){
System.out.print(" "+i);
}
}
}
}
Tuesday, December 4, 2007
Thursday, November 29, 2007
Explanation : public static void main(String args[])
- public : The public method can be accessed outside the class
- static : We must have an instance to access the class method.
- void : There is no need by application to return value. JVM launcher do this with it exists.
- main() : It is the entry point for the java application.
- String args[] : It holds optional arguments passed to the class.
Subscribe to:
Posts (Atom)