You are on page 1of 14

JAVA PROGRAMS

1. To Find the Biggest Number.


import java.io.*;
class Largestinset
{
public static void main(String args[])
{
int x[]={1,3,5,7,9,13,15,17,19};
int largest=x[0];
int n=x.length;
for(int i=0;i<n;i++)
{
if(x[i]>largest)largest=x[i];
}
System.out.println("Largest of these is="+largest);
}
}

Output:
C:\jdk1.8\bin>javac Largestinset.java
C:\jdk1.8\bin>java Largestinset
Largest of these are=19

2. To Find Factorial Using Java Application.


import java.io.*;
class Factorial
{
public static void main(String args[])
{
int factorial=1;
int n=3;
for(int i=1;i<=n;i++)
{
factorial=factorial*i;
}
System.out.println("Factorial is="+factorial);
}
}

Output:
C:\jdk1.8\bin>javac Factorial.java
C:\jdk1.8\bin>java Factorial
Factorial is=6

3. To Print the numbers in Floyd's Triangle.


class BinTri
{
public static void main(String args[])
{
int i,j,c;
c=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if((c%2)==0)
{
System.out.print(0+" ");
}
else
{
System.out.print(1+" ");
}
c++;
}
System.out.println();
}
}
}

Output:
C:\jdk1.8\bin>javac BinTri.java
C:\jdk1.8\bin>java BinTri
1
01
010
1010
10101

4. To Print using if...Else if Statement.


import java.io.*;
class Largest
{
public static void main(String args[])
{
int x=100, y=50, z=150;
int largest;
if(x>y)
{
if(x>z)largest=x;
else largest=z;
}
else
{
if(y>z)largest=y;
else largest=z;
}
System.out.println ("Largest of the three numbers is="+largest);
}
}

Output:
C:\jdk1.8\bin>javac Largest.java
C:\jdk1.8\bin>java Largest
Largest of the three numbers is=150

5. To Test SWITCH command


class CityGuide
{
public static void main(String args[])
{
char choice;
System.out.println("Select your choice");
System.out.println(" M----> Madras");
System.out.println(" B----> Bombay");
System.out.println(" C----> Calcutta");
System.out.print("choice---->");
System.out.flush();
try
{
switch(choice = (char) System.in.read())
{
case 'M':
case 'm': System.out.println("Madras:Booklet5");
break;
case 'B':
case 'b': System.out.println("Bombay:Booklet9");
break;
case 'C':
case 'c': System.out.println("Calcutta:Booklet15");
break;
default: System.out.println("Invalid choice(IC)");
}
}
catch (Exception e)
{
System.out.println("I/O Error");
}
}
}

Output:
C:\jdk1.8\bin>javac CityGuide.java
C:\jdk1.8\bin>java CityGuide
Select your choice
M----> Madras
B----> Bombay
C----> Calcutta
choice---->M
Madras:Booklet5
5

6. To Count the Number of Even and Odd Number


import java.io.*;
class evenodd
{
public static void main(String args[])
{
int number[] ={2,5,7,8,15};
int even =0;
int odd =0;
for (int i=0; i<number.length;i++)
{
if ((number[i]%2)==0)
{
even +=1;
System.out.println(number[i]+" is even");
}
else
{
odd +=1;
System.out.println(number[i]+" is odd");
}
}
System.out.println("even numbers are:"+even + "odd numbers:"+odd);
}
}

Output:
C:\jdk1.8\bin>javac evenodd.java
C:\jdk1.8\bin>java evenodd
2 is even
5 is odd
7 is odd
8 is even
15 is odd
even numbers are:2 odd numbers:3

7. To Print the Result Using Elseif Statement


import java.io.*
class Largest
{
public static void main(String args[])
{
int x=100;
int y=150;
int z=200;
int largest;
if(x>y)
{
if(x>z)largest=x;
else largest=z;
}
else
{
if(y>z)largest=y;
else largest=z;
}
System.out.println("Largest of the three number is="+largest);
}
}

Output:
C:\jdk1.8\bin>javac Largestof3.java
C:\jdk1.8\bin>java Largest
Largest of the three number is=200

8. To Sort the Numbers in a Given Array


import java.io.*;
class sorting
{
public static void main(String args[])
{
int temp,i,j;
int x[]={3,10,14,17,21,30,37,8,12};
int n=x.length;
for (i=0; i<n;i++)
{
for (j=i+1; j<n;j++)
{
if (x[i]>x[j])
{
//swap values
temp= x[i];
x[i]= x[j];
x[j]= temp;
}
}//end for j
/*print values after each iteration*/
System.out.println("After iteration"+(i+1)+", the values are");
for(int k=0;k<n;k++) System.out.print(" "+x[k]);
System.out.println("");
}//end for i
System.out.println("");
System.out.println("");
System.out.println("Sorted numbers are:");
for (i=0; i<n;i++)
System.out.print(" "+x[i]);
System.out.println();
}
}

Output:
C:\jdk1.8\bin>javac sorting.java
C:\jdk1.8\bin>java sorting
After iteration1, the values are
3 10 14 17 21 30 37 8 12
After iteration2, the values are
3 8 14 17 21 30 37 10 12
After iteration3, the values are
3 8 10 17 21 30 37 14 12
After iteration4, the values are
3 8 10 12 21 30 37 17 14
After iteration5, the values are
3 8 10 12 14 30 37 21 17
After iteration6, the values are
3 8 10 12 14 17 37 30 21
After iteration7, the values are
3 8 10 12 14 17 21 37 30
After iteration8, the values are
3 8 10 12 14 17 21 30 37
After iteration9, the values are
3 8 10 12 14 17 21 30 37
Sorted numbers are:
3 8 10 12 14 17 21 30 37

9. To Print the String in APPLET VIEWER


import java.awt.*;
import java.applet.*;
/* <applet code="HelloParam" width=300 height=300>
</applet>*/
public class HelloParam extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Applet!", 10, 100);
}
}

Output:
javac Face.java
appletviewer Face.java

10

10. To Draw Different Shapes Using APPLET VIEWER


import java.awt.*;
import java.applet.*;
public class LineRect extends Applet
{
public void paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.setColor(Color.blue);
g.fillRect(60,10,30,80);
g.setColor(Color.green);
g.drawRoundRect(10,100,80,50,10,10);
g.setColor(Color.yellow);
g.fillRoundRect(20,110,60,30,5,5);
g.setColor(Color.red);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
g.drawString("Line Rectangles Demo",65,180);
g.drawOval(230,10,200,150);
g.setColor(Color.blue);
g.fillOval(245,25,100,100);
}
}
/*<APPLET
CODE = LineRect.class
WIDTH = 250
HEIGHT = 200>
</APPLET>*/

Output:
javac Face.java
appletviewer Face.java

11

11. To Draw the Faces using APPLET VIEWER


import java.awt.*;
import java.applet.*;
public class Face extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.drawOval(85,100,30,30);
g.fillArc(60,125,80,40,180,180);
g.drawOval(25,92,15,30);
g.drawOval(160,92,15,30);
}
}
/*<APPLET
CODE = Face.class
WIDTH = 250
HEIGHT = 200>
</APPLET>*/

Output:
javac Face.java
appletviewer Face.java

12

12. To Take Input Value in Text Field and to Show the Results in
VIEWER.

APPLET

import java.awt.*;
import java.applet.*;
public class UserIn extends Applet
{
TextField text1, text2;
public void init()
{
text1 = new TextField(8);
text2 = new TextField(8);
add (text1);
add (text2);
text1.setText ("0");
text2.setText ("0");
}
public void paint(Graphics g)
{
int x=0, y=0, z=0;
String s1,s2,s;
g.drawString("Input a number in each box ", 10, 50);
try
{
s1 = text1.getText();
x = Integer.parseInt(s1);
s2 = text2.getText();
y = Integer.parseInt(s2);
}
catch (Exception ex) {}
z = x + y;
s = String.valueOf(z);
g.drawString("THE SUM IS: ", 10, 75);
g.drawString(s,100,75);
}
public boolean action(Event event, Object obj)
{
repaint();
return true;
}
}
<APPLET
CODE = UserIn.class

13

WIDTH = 250
HEIGHT = 200>
</APPLET>

Output:
javac UserIn.java
appletviewer UserIn.html

14

You might also like