JAVA PROGRAMMING LAB UNIPOWER
Exp:1
Aim: Write a java program to find the factorial of the number.
Program:
class Fact
{
public static void main(String[] args)
{
int number = 5;
int factorial = number;
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
[Link]("Factorial of a number is:" + factorial);
}
}
Output: Factorial of a number is:120
JAVA PROGRAMMING LAB UNIPOWER
Exp:2
Aim :Write a java program to display Fibonacci series .
Program:
import [Link];
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner([Link]);
[Link]("Enter value of n:");
n = [Link]();
[Link]("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
[Link](a+" ");
}
}
}
Output:
Enter value of n:5
Fibonacci Series:01123
JAVA PROGRAMMING LAB UNIPOWER
Exp:3
Aim :Write a java program to find whether the give number is Armstrong number or not.
Program:
import [Link].*;
import [Link].*;
class ArmStrong
{
public static void main(String[] args)throws IOException
{
int t,s=0,n,r;
[Link]("Enter number:");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
n=[Link]([Link]());
t=n;
while( t!=0)
{ r=t%1
0;
s=s+r*r*r;
t=t/10;
}
if(n==s)
[Link]("Number given is Armstrong");
else
[Link]("Number given is not Armstrong");
}
}
Output:
Enter number:153
Number given is Armstrong
JAVA PROGRAMMING LAB UNIPOWER
Exp:4
Aim :Write a java program to reverse the given string.
Program:
import [Link].*;
import [Link].*;
class RevStr
{
public static void main(String[] args) throws IOException
{
String or, rev="";
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter String:");
or=[Link]();
int len=[Link]();
for(int i=len-1;i>=0;i--)
{
rev=rev+[Link](i);
}
[Link]("Reverse of a string:"+rev);
}
}
Output:
Enter String:siva
Reverse of a string:avis
JAVA PROGRAMMING LAB UNIPOWER
Exp:5
Aim: Write a java program to evaluate quadratic equation
Program:
import [Link].*;
class QudEq
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int a,b,c;
double x,y,z;
[Link]("ENTER a VALUE:");
br= new BufferedReader(new InputStreamReader([Link]));
a=[Link]([Link]());
[Link]("ENTER b VALUE:");
b=[Link]([Link]());
[Link]("ENTER c VALUE:");
c=[Link]([Link]());
z=[Link](b*b-4*a*c);
x=(-b+[Link](b*b-4*a*c))/(2*a);
y=(-[Link](b*b-4*a*c))/(2*a);
if(z>0)
{
[Link]("THE REAL SOLUTIONS ARE ="+x+"\t"+"Y="+y);
}
else
{
[Link]("THERE ARE NO REAL SOLUTIONS");
}
}
}
Output:
Enter a value:
4
Enter b value:
6
Enter c value:
-2
THE REAL SOLUTIONS ARE X=0.28077640640441515 Y==-1.7807764064044151
JAVA PROGRAMMING LAB UNIPOWER
Exp:6
Aim :Write a java program to print Fibonacci series for the given number using recursion
Program:
import [Link].*;
public class FibRec
{
static int fib(int i)
{
if(i==1||i==2)
{
return i-1;
}
else
{
return fib(i-1)+fib(i-2);
}
}
public static void main(String args[])throws IOException
{
BufferedReader br;
int n;
[Link]("enter n value:");
br = new BufferedReader(new InputStreamReader([Link]));
n=[Link]([Link]());
[Link]("FIBONACCI SEQUENCE:");
for(int i=2;i<=n+1;i++)
{
[Link](fib(i)+"\t");
}
}
}
Output:
enter n value:
5
FIBNOCCI SEQUENCE:1 1 2 3 5
JAVA PROGRAMMING LAB UNIPOWER
Exp:7
Aim: Write a java program to print Fibonacci series for the given number using non-recursion
Program:
import [Link].*;
class FibNonRec
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int a=0;
int b=1,t,n;
br=new BufferedReader(new InputStreamReader([Link]));
[Link]("enter n value:");
n=[Link]([Link]());
[Link]("FIBONACCI SEQENCE IS:");
[Link](a+"\t");
do
{
[Link](b+"\t");
t=b;
b=a+b;
a=t;
}while(b<=n);
}
}
Output:
enter n value:
5
FIBNOCCI SEQUENCE IS: 0 1 1 2 3 5
JAVA PROGRAMMING LAB UNIPOWER
Exp;8
Aim: Write a java program to count number of words in a given text
Program:
import [Link].*;
import [Link].*;
class CountWords
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a string : ");
String str=[Link]();
int count=0;
if([Link]()==0)
{
[Link]("[Link] words in given text:" + count);
}
else
{
for(int i=0;i<[Link]();i++)
{
if([Link](i)==' ')
{
count++;
}
}
[Link]("[Link] words in given text:" + (count+1));
}
}
}
Output:
Enter a string:
hi this is siva
[Link] words in given text:
4
JAVA PROGRAMMING LAB UNIPOWER
Exp:9
Aim: Write a java program to check the given string is palindrome or not
Program:
import [Link].*;
public class Palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter String:");
String str=[Link]();
char ch[]=new char[[Link]()];
for(int i=[Link]()-1,j=0;i>=0;i--,j++)
ch[j]=[Link](i);
String restr=new String(ch);
[Link]("Reverse of String "+str+" is "+restr);
if([Link](restr)==0)
[Link](str+" "+"is a Palindrome");
else
[Link](str+" "+"is not a Palindrome");
}
}
Output:
Enter String:
madam
Reverse of String madam is madam
madam is a Palindrome
JAVA PROGRAMMING LAB UNIPOWER
Exp:10
Aim: Write a java program to print the prime numbers upto nth number
Program:
import [Link].*;
class Primenos
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int i,t,flag,n;
br=new BufferedReader(new InputStreamReader([Link]));
[Link]("ENTER n VALUE:");
n=[Link]([Link]());
[Link]("PRIME NUMBERS UP TO"+" "+n+":");
for(i=2;i<=n;i++)
{
flag=1;
for(t=2;t<i;t++)
{
if(i%t==0)
{
flag=0;
break;
}
}
if(flag==1)
[Link](i);
}
}
}
Output:
ENTER n VALUE:
5
PRIME NUMBERS UP TO 5:
2
3
5
JAVA PROGRAMMING LAB UNIPOWER
Exp:11
Aim: To read a line of integers and display each integer and sum of all integers
Program:
import [Link].*;
import [Link].*;
class StrTok
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter String in numbers");
String str=[Link]();
StringTokenizer s=new StringTokenizer(str);
int x,sum=0;
while([Link]())
{
String str1=[Link]();
x=[Link](str1);
[Link](x);
sum+=x;
}
[Link]("total sum:"+sum);
}
}
Output:
Enter String in numbers
385
3
8
5
total sum: 16
JAVA PROGRAMMING LAB UNIPOWER
Exp:12
Aim: To develop an applet in java that receives an integer in one text field and computes its
factorial in value and returns it in another text field ,when the button named “Compute” is clicked.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code=Fact width=500 height=500>
</applet>*/
public class Fact extends Applet implements ActionListener
{
Button b1,b2;
Label l1,l2;
TextField tf1,tf2;
public void init()
{
b1=new Button("COMPUTE");
[Link](this);
b2=new Button("CLEAR");
[Link](this);
tf1=new TextField(20);
tf2=new TextField(20);
l1=new Label("NUMBER");
l2=new Label("RESULT");
// setLayout(new GridLayout(3,2));
add(l1);
add(tf1);
add(l2);
add(tf2);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==b1)
{
int a=[Link]([Link]());
int fact=1;
for(int i=1;i<=a;i++)
fact*=i;
[Link](""+fact);
}
else
{
[Link]("");
JAVA PROGRAMMING LAB UNIPOWER
[Link]("");
}
}
}
Output:
JAVA PROGRAMMING LAB UNIPOWER
Exp:13
Aim:Write a Java program that creates a user interface to perform integer divisions. The user
enters two numbers in the text fields, Num1 andNum2. The division of Num1 and Num2 is
displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an
integer, the program would throw a Number Format Exception. If Num2 were Zero, the program
would throw an Arithmetic Exception. Display the exception in a message dialog box.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
class SampleDialog extends Dialog implements ActionListener
{
SampleDialog(Frame parent,String title,String msg)
{
super(parent,title,false);
setLayout(new FlowLayout());
setSize(300,200);
add(new Label(msg));
Button b;
add(b=new Button("OK"));
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
}
public class Division extends Frame implements ActionListener
{
Button b1,b2;
Label l1,l2,l3;
TextField tf1,tf2,tf3;
Division()
{
super("Exception Handler");
b1=new Button("DIVIDE");
[Link](this);
b2=new Button("CLEAR");
[Link](this);
tf1=new TextField(15);
tf2=new TextField(15);
tf3=new TextField(20);
JAVA PROGRAMMING LAB UNIPOWER
l1=new Label("Enter the Numerator");
JAVA PROGRAMMING LAB UNIPOWER
l2=new Label("Enter the Denaminator");
l3=new Label("RESULT");
setLayout(new FlowLayout());
add(l1);
add(tf1);
add(l2);
add(tf2);
add(l3);
add(tf3);
add(b1);
add(b2);
addWindowListener (new WindowAdapter()
{ public void windowClosing (WindowEvent e)
{
[Link](0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==b1)
{
try
{
int a=[Link]([Link]());
int b=[Link]([Link]());
int c=a/b;
[Link](""+c);
}
catch(ArithmeticException ex)
{
[Link]("--");
SampleDialog d=new SampleDialog(this,"EXCEPTION","DIVIDE BY ZERO");
[Link](true);
}
catch(Exception ex)
{
SampleDialog d=new SampleDialog(this,"EXCEPTION",""+ex);
[Link](true);
}
}
else
{
JAVA PROGRAMMING LAB UNIPOWER
[Link]("");
[Link]("");
[Link]("");
}
}
public static void main(String args[])
{
Division b=new Division();
[Link](300,300);
[Link](true);
}
}
Output:
JAVA PROGRAMMING LAB UNIPOWER
Exp:14
Aim:Write a Java program that implements a multi-thread application that has three threads.
First thread generates random integer every 1 second and if the value is even, second
thread computes the square of the number and prints. If the value is odd, the third thread
will print the value of cube of the number.
Program:
import [Link].*;
class Even implements Runnable
{
public int x;
public Even(int x)
{
this.x = x;
}
public void run()
{
[Link]("New Thread "+ x +" is EVEN and Square of " + x + " is: " +
x * x);
}
}
class Odd implements Runnable
{
public int x;
public Odd(int x)
{
this.x = x;
}
public void run()
{
[Link]("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x *
x * x);
}
}
class A extends Thread
{
public void run()
{
int num = 0;
Random r = new Random();
try
{
for (int i = 0; i < 5; i++)
{
JAVA PROGRAMMING LAB UNIPOWER
num = [Link](100);
[Link]("Main Thread and Generated Number is " +
num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new Even(num));
[Link]();
}
else
{
Thread t2 = new Thread(new Odd(num)); [Link]();
}
[Link](1000);
[Link](" ");
}
}
catch (Exception ex)
{
[Link]([Link]());
}
}
}
public class ThreeThreadDemo
{
public static void main(String[] args)
{
A a = new A();
[Link]();
}
}
Output:
Main Thread and Generated Number is 87
New Thread 87 is ODD and Cube of 87 is: 658503
Main Thread and Generated Number is 6
New Thread 6 is EVEN and Square of 6 is: 36
Main Thread and Generated Number is 3
New Thread 3 is ODD and Cube of 3 is: 27
Main Thread and Generated Number is 80
New Thread 80 is EVEN and Square of 80 is: 6400
Main Thread and Generated Number is 1
New Thread 1 is ODD and Cube of 1 is: 1
UNIPOWER
JAVA PROGRAMMING LAB
Exp:15
Aim:Write a Java program for the following:
i. Create a doubly linked list of elements.
ii. Delete a given element from the above list.
iii. Display the contents of the list after deletion.
Program:
import [Link];
/* Class Node */
class Node
{
protected int data;
protected Node next, prev;
/* Constructor */
public Node()
{
next = null;
prev = null;
data = 0;
}
/* Constructor */
public Node(int d, Node n, Node p)
{
data = d;
next = n;
prev = p;
}
/* Function to set link to next node */
public void setLinkNext(Node n)
{
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Node p)
{
prev = p;
}
/* Funtion to get link to next node */
public Node getLinkNext()
{
return next;
}
/* Function to get link to previous node */
UNIPOWER
JAVA PROGRAMMING LAB
public Node getLinkPrev()
{
return prev;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at begining */
public void insertAtStart(int val)
{
Node nptr = new Node(val, null, null);
if(start == null)
UNIPOWER
JAVA PROGRAMMING LAB
{
start = nptr;
end = start;
}
else
{
[Link](nptr);
[Link](start);
start = nptr;
}
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
[Link](end);
[Link](nptr);
end = nptr;
}
size++;
}
/* Function to insert element at position */
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null, null);
if (pos == 1)
{
insertAtStart(val);
return;
}
Node ptr = start;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node tmp = [Link]();
[Link](nptr);
UNIPOWER
JAVA PROGRAMMING LAB
[Link](ptr);
[Link](tmp);
[Link](nptr);
}
ptr = [Link]();
}
size++ ;
}
/* Function to delete node at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
if (size == 1)
{
start = null;
end = null;
size = 0;
return;
}
start = [Link]();
[Link](null);
size--;
return ;
}
if (pos == size)
{
end = [Link]();
[Link](null);
size-- ;
}
Node ptr = [Link]();
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node p = [Link]();
Node n = [Link]();
[Link](n);
[Link](p);
size-- ;
return;
}
ptr = [Link]();
}
}
UNIPOWER
JAVA PROGRAMMING LAB
/* Function to display status of list */
public void display()
{
[Link]("\nDoubly Linked List = ");
if (size == 0)
{
[Link]("empty\n");
return;
}
if ([Link]() == null)
{
[Link]([Link]() );
return;
}
Node ptr = start;
[Link]([Link]()+ " <-> ");
ptr = [Link]();
while ([Link]() != null)
{
[Link]([Link]()+ " <-> ");
ptr = [Link]();
}
[Link]([Link]()+ "\n");
}
}
/* Class DoublyLinkedList */
public class DoublyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
/* Creating object of linkedList */
linkedList list = new linkedList();
[Link]("Doubly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
[Link]("\nDoubly Linked List Operations\n");
[Link]("1. insert at begining");
[Link]("2. insert at end");
[Link]("3. insert at position");
[Link]("4. delete at position");
[Link]("5. check empty");
[Link]("6. get size");
UNIPOWER
JAVA PROGRAMMING LAB
int choice = [Link]();
switch (choice)
{
case 1 :
[Link]("Enter integer element to insert");
[Link]( [Link]() );
break;
case 2 :
[Link]("Enter integer element to insert");
[Link]( [Link]() );
break;
case 3 :
[Link]("Enter integer element to insert");
int num = [Link]() ;
[Link]("Enter position");
int pos = [Link]() ;
if (pos < 1 || pos > [Link]() )
[Link]("Invalid position\n");
else
[Link](num, pos);
break;
case 4 :
[Link]("Enter position");
int p = [Link]() ;
if (p < 1 || p > [Link]() )
[Link]("Invalid position\n");
else
[Link](p);
break;
case 5 :
[Link]("Empty status = "+ [Link]());
break;
case 6 :
[Link]("Size = "+ [Link]() +" \n");
break;
default :
[Link]("Wrong Entry \n ");
break;
}
/* Display List */
[Link]();
[Link]("\nDo you want to continue (Type y or n) \n");
ch = [Link]().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
UNIPOWER
JAVA PROGRAMMING LAB
Output:
UNIPOWER
JAVA PROGRAMMING LAB