Showing posts with label java program. Show all posts
Showing posts with label java program. Show all posts

Thursday, February 9, 2012

Java Prime Number Program - Check Number is Prime or Not

It is sample java example of prime number. In this java program user input any number. Program checks accepted number is prime number or not a prime number.

Program Code
//Java Prime Number Program
import java.util.*;
class PRIME
{
 int prime(int n)
 {
  int i;
  for(i=2;i<=n/2;i++)
  {
   if((n%i)==0)
   {
    break;
   }
  }
  if(i>n/2 && n!=1)
  return 1;
  return 0;
 }
}
public class PRIME_MAIN
{
 public static void main(String args[])
 {
  PRIME p1=new PRIME();
  Scanner object1=new Scanner(System.in);
  System.out.println("");
  System.out.println("******PRIME NUMBER PROGRAM******");
  System.out.print("Enter any number: ");
  int num=object1.nextInt();
  int flag=p1.prime(num);
  if(flag==1)
  System.out.println(num+" is a prime number");
  else
  System.out.println(num+" is not a prime number");
 }
}


Output
Copy code and paste into notepad and save as "PRIME_MAIN.java".



Sunday, December 25, 2011

Java is a Platform Independent Language

Meaning of Platform Independent

It means that a program of java language must be portable to other system. Java program must be run on any hardware, machine and any operating system.
Example: java mobile games
Java mobile games runs on any multimedia mobile. When we start a java game on mobile first automatically load java runtime environment. Then mobile able to run java game independently.

Why java is a platform independent language?

Java is platform independent language. When we compile java program then it makes source file to bytecode(class file).



Bytecode is a intermediate form which is closer to machine representation. Bytecode is certain and remain same for all platforms. When compile source code(program.java) is makes bytecode(program.class). Bytecode is faster then source code.
To run bytecode to any platform there is need to load java virtual machine(JVM) interpreter.
Before run java program need JVM must be loaded into particular system. JVM makes java runtime environment into system. Bytecode makes java as platform independent compilation.


Hope, you like this post. If known further details then share to others using comments. :)