Java Pragrams
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Main
{
// Your program begins with a call to main().
public static void main(String args[])
{
System.out.println("This is a simple Java program.");
}
}
//area of a circle
class AreaOfCircle
{
public static void main(String s[])
{
double pi, r, a;
pi = 3.14;
r = 10;
a = pi * r * r;
System.out.println("Area of circle = " + a);
}
}
//char data type example
class CharDemo
{
public static void main(String s[])
{
char ch1;
ch1 = 'A';
System.out.println("ch1 = " + ch1);
ch1 = 65; //A
System.out.println("ch1 = " + ch1);
ch1++; // increment ch1
System.out.println("ch1 = " + ch1);
}
}
//Type casting example
class Conversion
{
public static void main(String s[])
{
byte b;
int i = 257;
double d = 3.14;
System.out.println("\n\nConversion of int to byte.");
b = (byte)i;
System.out.println("i = " + i);
System.out.println("b = " + b);
System.out.println("\n\nConversion of double to int.");
i = (int)d;
System.out.println("d = " + d);
System.out.println("i = " + i);
}
}
class Average
{
public static void main(String s[])
{
double a[] = { 10, 20, 30, 40, 50, 60 };
double sum = 0;
for (int i = 0; i < 6; i++)
sum = sum + a[i];
double avg = sum / 6;
System.out.println("Average = " + avg);
}
}
More Java Programs Click Below Link