Tuesday, July 1, 2008


Java Prime Number Example

What is Prime Number?

Definition : A prime number is a natural number that has only two divisor. Either it is divisible by 1 or itself and it is greater than 1. So we can say it has only one divisor means it can only divided by itself.

Here is the example of prime number in Java. It display all the prime numbers between 1 and the limit entered by user. Get the source code of prime number example here :

import java.io.*;
class Primenumberexample {
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter Limit to Print Prime Number :");
int k = Integer.parseInt(bf.readLine());
System.out.println("Prime Numbers Are : ");
for (int i=1; i < k; i++ ){
for (int j=2; j < i; j++){
int a = i%j;
if (a==0){
break;
}
}
if(i == j){
System.out.print(" "+i);
}
}
}
}


Monday, June 30, 2008


How to Calculate Area of Rectangle?

This example explain you how to calculate area of a rectangle using java code. For calculating area of rectangle, we need three variables. One for length, one for breadth and one variable calculate the area of rectangle. We have already read and know how to calculate area of rectangle using formula a=lXb. Here is the source code to find the area of rectangle :

class Rectangleexample
{
public static void main(String[] args)
{
int len=5, breadth=7, area=1;
area = len*breadth;
System.out.println("Area if Rectangle :" + area);
}
}