1) Prints the Welcome

public class simple

{

public static void main(String args[])

{

System.out.println(“Welcome!”);

}

}

 

Output:

Welcome

 

2) Calling the greet method from another class

public class Greeting

{

/* Defining Methods*/

public void greet()

{

System.out.println(“Hi! Welcome”);

}

}

 

public class TestGreeting

{

public static void main(String args[])

{

/* Creating objects */

Greeting hello = new Greeting();

/* Calling the method using objects  from another class*/

hello.greet();

}

}

 

 

Output:

Hi! Welcome

 

3)Compute the biggest number among three numbers

 

class biggestnumber

{

public static void main(String args[])

{

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

int c = Integer.parseInt(args[2]);

if((a>b)&&(a>c))

System.out.println(“The biggest among the three numbers is:” +a);

else if((b>a)&&(b>c))

System.out.println(“The biggest among the three numbers is:” +b);

else

System.out.println(“The biggest among the three numbers is:” +c);

}

}

 

Output:

java biggestnumber 23 30 20

The biggest among the three numbers is:30

 

 

4) Prints the values stored in the primitive types and string

public class sample

{

public static void main(String args[])

{

int a=10;

float b=10000.0F;

String name= “Vignesh karthick”;

System.out.println(“Integer a:” +a);

System.out.println(“Float b:” +b);

System.out.println(“String name:” + name);

}

}

Output:

 

Integer a:10

Float b:10000.0

String name:Vignesh karthick

 

5) Computation distance light travels using long variables.

 

class Light

{

public static void main(String args[])

{

int lightspeed;

long days;

long seconds;

long distance;

// approximate speed of light in miles per second

lightspeed = 186000;

days = 1000; // specify number of days here

seconds = days * 24 * 60 * 60; // convert to seconds

distance = lightspeed * seconds; // compute distance

System.out.print(“In ” + days);

System.out.print(” days light will travel about “);

System.out.println(distance + ” miles.”);

}

}

Output:

In 1000 days light will travel about 16070400000000 miles.

 

6) Factorial of a given number

import java.io.*;

class factorial

{

 

public static void main(String args[])throws IOException

{

int i,n,f=1;

DataInputStream in=new DataInputStream(System.in);

System.out.println(“Enter the number:”);

n=Integer.parseInt(in.readLine());

System.out.println(“The factorial of” +n+ “is” );

for(i=1;i<=n;i++)

{

f=f*i;

}

System.out.println(f);

}

}

 

 

Output:

Enter the number:

5

The factorial of 5 is

120

 

 

7) Swapping of 2 numbers with third variable

class SwappingWithThirdVariable

{

public static void main(String args[])

{

int num1=50;

int num2=20;

System.out.println(“Before Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

swap(num1,num2);

}

public static void swap(int num1,int num2)

{

int temp;

temp=num1;

num1=num2;

num2=temp;

System.out.println(“After Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

}

}

Output:

Before Swapping

num1:50

num2:20

After Swapping

num1:20

num2:50

 

8) Swapping of 2 numbers without third variable

 

class SwappingWithOutThirdVariable

{

public static void main(String args[])

{

int num1=10;

int num2=20;

System.out.println(“Before Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

swap(num1,num2);

}

public static void swap(int num1,int num2)

{

num1=num1+num2;

num2=num1-num2;

num1=num1-num2;

System.out.println(“After Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

}

}

Output:

 

Before Swapping

num1:10

num2:20

After Swapping

num1:20

num2:10

 

 

9) Fibonnaci Series for a given number of times

import java.io.*;

class fibonacci

{

public static void main(String args[])throws IOException

{

int f1=-1,f2=1,f3,i,n;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Enter the limit:”);

n=Integer.parseInt(in.readLine());

System.out.println(“Fibonacci Series are:”);

for(i=1;i<=n;i++)

{

f3=f1+f2;

f1=f2;

f2=f3;

System.out.println(f3);

}

}

}

 

 

 

Output:

Enter the limit:

7

Fibonacci Series are:

0

1

1

2

3

5

8

 

10) Calculation of Simple Interest

import java.io.*;

class simpleinterest

{

public static void main(String args[])throws IOException

{

int p,n;

float r,si,amt;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Enter the principle amount:”);

p=Integer.parseInt(in.readLine());

System.out.println(“Enter the no. of years:”);

n=Integer.parseInt(in.readLine());

System.out.println(“Enter the rate of interest:”);

r=Float.parseFloat(in.readLine());

si=(p*n*r)/100;

amt=p+si;

System.out.println(“Simple Interest:” +si);

System.out.println(“Amount:” +amt);

}

}

 

 

Output:

 

Enter the principle amount:

1000

Enter the no. of years:

2

Enter the rate of interest:

2.5

Simple Interest:50.0

Amount:1050.0

 

11) Computation for Area of a Circle

 

class DoubleDemo

{

public static void main(String args[]) {

double pi, r, a;

r = 10.8; // radius of circle

pi = 3.1416; // pi, approximately

a = pi * r * r; // compute area

System.out.println(“Area of circle is ” + a);

}

}

 

Output:

 

Area of circle is 366.436224

 

12) Check whether the given number is prime  or not

public static void main(String args[])throws IOException

{

int i,num,remainder;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Enter the number:”);

num=Integer.parseInt(in.readLine());

for(i=2;i<num;i++)

{

remainder=num%i;

if(remainder==0)

{

System.out.println(num + ” is not prime”);

break;

}

}

if(i==num)

{

System.out.println(num + ” is  prime”);

}

}

}

 

Output:

 

Enter the number:

12

12 is not prime

 

13) Compute rightangledtriangle

 

 

import java.io.*;

public class rightangledtriangle

{

public static void main(String[] args) throws IOException

{

DataInputStream in = new DataInputStream(System.in);

int n;

System.out.println(“Enter the no. of rows:”);

n=Integer.parseInt(in.readLine());

for(int i=1;i<=n;i++)

{

for(int j=1;j<=i;j++)

{

System.out.print(” “+i+” “);

}

System.out.print(“\n”);

}

}

}

 

Output:

Enter the no. of rows:

5

The right angled triangle:

1

2  2

3  3  3

4  4  4  4

5  5  5  5  5