You are on page 1of 6

Impotant Java Programs

 Java program to display a welcome message:


class welcome
{
public static void main(String args[])
{
System.out.println("Welcome to java");
}
}

 Java program to find Call Cost:


public class CallCost
{
public static void main(String[] args)
{
double balance;
double rate;
double duration;
double cost;
balance = 399.99;
rate = 1.02;
duration = 37;
cost = duration * rate;
balance = balance - cost;
System.out.println("Call Duration:");
System.out.println("duration");
System.out.println("Seconds");
System.out.println("Balance:"+balance+"Rupees");
}
}

 Java program to find the given number is even or odd:


class even
{
public static void main(String args[])
{
int number=278;
if(number%2==0)
{
System.out.println("The given number is an even number");
}
else
{
System.out.println("The given number is an odd number");
}
}
}
 Java program to find Simple Interest:
class Interest
{
public static void main(String args[])
{
double principal;
double rate;
double duration;
double interest;
double maturity;
principal=25000;
rate=12;
duration=5;
interest=principal*rate*duration;
maturity= principal+interest;
System.out.println("Principal amount: "+principal+" Rupees");
System.out.println("Deposit for duration of: "+duration+" Years");
System.out.println("Interest rate: "+rate+" %");
System.out.println("Interest amount: "+interest+" Rupees");
System.out.println("Maturity amount: "+maturity+" Rupees");
}
}

 Java program to perform different Arithmetic opreations:

class ArithOperators
{
public static void main (String[] args)
{
short x =6;
int y =4;
float a=12.5f;
float b=7.2f;
System.out.println ("x is"+x+",y is"+y);
System.out.println ("x +y="+x+y);
System.out.println ("x-y="+(x-y));
System.out.println ("x /y="+(x/y));
System.out.println ("x %y="+(x%y));
x=-6;
System.out.println ("x %y="+(x%y));
y=-4;
System.out.println ("x %y="+(x%y));
x=6; y=-4;
System.out.println ("x %y="+(x%y));
System.out.println ("a is"+a+",b is"+b);
System.out.println ("a/b="+(a/b));
System.out.println ("a/x="+(a/x));
System.out.println ("a%x="+(a%x));
System.out.println ("a%b="+(a%b));
}
}
 Java program to find the given year is leap year or not:
class leapyear
{
public static void main(String args[])
{
int year=2016;
if(year%4==0)
{
System.out.println("The given year is a leap year");
}
else
{
System.out.println("The given year is not a leap year");
}
}
}

 Java program to display 1 to 100 numbers:


class num
{
public static void main(String args[])
{
int i;
for(i=1;i<=100;i++)
{
System.out.println(i);
}
}
}

 Java program to display even numbers between 1 to 100:


class even
{
public static void main(String args[])
{
int i;
for(i=2;i<=100;i++)
{
if(i%2==0)
{
System.out.println(i);
}
}
}
}
 Java program to display odd numbers between 1 to 100:
class odd
{
public static void main(String args[])
{
int i;
for(i=1;i<=100;i++)
{
if(i%2!=0)
{
System.out.println(i);
}
}

}
}
 Java program to find the student is pass or fail:

class student
{
public static void main(String args[])
{
int mark=32;
if(mark>=33)
{
System.out.println("The student is pass with"+mark+"mark");
}
else
{
System.out.println("The student is fail with "+mark+" mark");
}
}
}

 Java program to find Area of a circle:


class Circle
{
public static void main(String args[])

{
double pi=3.14;
double r=6.5;
double area;
area=pi*r*r;
System.out.println("radius of circle is"+r);
System.out.println("Area of circle is"+area);
}

}
 Java program to find Perimeter of a circle:
class perimeter
{
public static void main(String args[])

{
double pi=3.14;
double r=6.5;
double peri;
peri=2*pi*r;
System.out.println("radius of circle is "+r);
System.out.println("Perimeter of circle is "+peri);
}

}
 Java program to display simple 1-D array:
class Array1
{
public static void main(String [] s)
{
int marks[];
marks = new int [7];
for (int i=0; i<7; i++ )
{
System.out.println ("marks [" + i +" ] is" + marks[i]);
}
}
}
 Java program to display 2-D array:
class Array2D
{
public static void main (String [] s)
{
int marks1[][] = { {50,60,70}, {35,30,50},{70,75,80},{80,65,90},{50,50,55} };
int [] [] marks2 = { {50,60,70},{35,30,50},{70,75,80},{80,65,90},{50,50,55} };
System.out.print ("2-D Array marks1\n:");
display(marks1,5,3);
System.out.print ("2-D Array marks2\n:");
display(marks2,5,3);
}
static void display(int arr[][], int rows, int cols)
{
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
System.out.print (arr[i][j] +"\t");
}
System.out.println();
}
}
 Java program to display String:
class String1
{
public static void main (String [] s)
{
char name [] = { 'j', 'a', 'v', 'a'};
String str1 = new String();
String str2 = new String(name);
String str3 = new String(name, 1,2);
String str4 = new String(str3);
System.out.println ("str1 is " + str1);
System.out.println ("str2 is " + str2);
System.out.println ("str3 is " + str3);
System.out.println ("str4 is " + str4);
}
}
 Java program to find total and average of an array:
class ArrayAvg
{
public static void main (String [] s)
{
double numbers [] = {10.5,20.6,30.8,15.5,17.3,25.5,27.2,20,30,18.5};
byte ctr;
double sum=0,avg;

System.out.println("list of numbers is");


for (ctr=0;ctr<10;ctr++)
{
System.out.println(numbers[ctr]);
sum = sum + numbers [ctr];
}
avg = sum/10;
System.out.println("\nSum of above numbers is"+sum);
System.out.println("\nAverage of above numbers is"+avg);
}

}
 Java program to compare two strings:
class String2
{
public static void main (String [] s)
{
String str1= " I like Java";
String str2 = " I like Java";
String str3 = new String("I love India");
String str4 = new String(str3);
System.out.println ("str1==str2: "+ (str1==str2));
System.out.println ("str3==str4: " + (str3==str4));
System.out.println ("str1: " + str1);
System.out.println ("str2: " + str2);
System.out.println ("str3: " + str3);
System.out.println ("str4 " + str4);
}
}

You might also like