JAVA PROGRAMS 201
0
Program-1
class j1
public static void main(String args[])
System.out.println("MY NAME IS:\tVENKATARAMAN.V\n");
System.out.println("MY FATHER NAME IS:\tSUBRAMANIYAN.G\n");
System.out.println("MY MOTHER NAME IS:\tRAJAPRABHA.V\n");
System.out.println("MY BROTHER NAME IS:\tLAKSHMINARAYANAN.V");
Output
C:\Java\JDK16~1.0\bin>java j1
MY NAME IS: VENKATARAMAN.V
MY FATHER NAME IS: SUBRAMANIYAN.G
MY MOTHER NAME IS: RAJAPRABHA.V
MY BROTHER NAME IS: LAKSHMINARAYANAN.V
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-2
/*TO FIND SUM OF THREE NUMBERS*/
class j2
public static void main(String args[])
int sum=0,num1=1,num2=20,num3=30;
sum=num1+num2+num3;
System.out.println("THE SUM IS"+sum);
Output:
C:\Java\JDK16~1.0\bin>javac j2.java
C:\Java\JDK16~1.0\bin>java j2
THE SUM IS 51
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-3
//TO FIND SUM OF 3 NOS USING COMMAND LINE ARGUMENT
import java.io.*;
class j3
public static void main(String args[])
int sum,num1,num2,num3;
/*args[] is of type string.So it has to be coverted into integers*/
num1=Integer.parseInt(args[0]);
num2=Integer.parseInt(args[1]);
num3=Integer.parseInt(args[2]);
sum=num1+num2+num3;
System.out.println("The sum="+sum);
Output
C:\Java\JDK16~1.0\bin>javac j3.java
C:\Java\JDK16~1.0\bin>java j3 5 6 7
The sum=18
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-4
//TO FIND SUM OF 3 NOS USING GET INPUT FROM USER
import java.io.*;
class j4
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int sum,num1,num2,num3;
System.out.println("ENTER 1ST NO");
num1=Integer.parseInt(in.readLine());
System.out.println("ENTER 2ND NO");
num2=Integer.parseInt(in.readLine());
System.out.println("ENTER 3RD NO");
num3=Integer.parseInt(in.readLine());
sum=num1+num2+num3;
System.out.println("THE SUM IS"+sum);
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output
C:\Java\JDK16~1.0\bin>javac j4.java
Note: j4.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\Java\JDK16~1.0\bin>java j4
ENTER 1ST NO
30
ENTER 2ND NO
40
ENTER 3RD NO
50
THE SUM IS 120
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Prg-5
//TO FIND ARITHMATIC OPERATION FOR TWO VALUES
import java.io.*;
class j5
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int a,b,c,d,e,f,g;
System.out.println("ENTER A VALUE");
a=Integer.parseInt(in.readLine());
System.out.println("ENTER B VALUE");
b=Integer.parseInt(in.readLine());
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
System.out.println("THE ADDTION IS::"+c);
System.out.println("THE SUBTRACTION IS::"+d);
System.out.println("THE MULTIPLICATOIN IS::"+e);
System.out.println("THE DIVISION IS::"+f);
System.out.println("THE MODULES IS::"+g);
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
}}}
Output
C:\Java\JDK16~1.0\bin>java j5
ENTER A VALUE
50
ENTER B VALUE
THE ADDTION IS::55
THE SUBTRACTION IS::45
THE MULTIPLICATION IS::250
THE DIVISION IS::10
THE MODULUES IS::0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program- 6
//TO FIND BIGGEST NUMBERS BY USING IFELSE STATEMENT
import java.io.*;
class j6
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int a,b;
System.out.println("ENTER THE A VALUE");
a=Integer.parseInt(in.readLine());
System.out.println("ENTER THE B VALUE");
b=Integer.parseInt(in.readLine());
if(a>b)
System.out.println("A IS BIG");
else
System.out.println("B IS BIG");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output
C:\Java\JDK16~1.0\bin>javac j6.java
C:\Java\JDK16~1.0\bin>java j6
ENTER THE A VALUE
ENTER THE B VALUE
A IS BIG
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-7
//TO FIND BIGGEST NUMBERS FOR GIVEN THREE VARIABLES BY USING
NESTEDIF STATEMENT
import java.io.*;
class j7
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int a,b,c;
System.out.println("ENTER THE A VALUE");
a=Integer.parseInt(in.readLine());
System.out.println("ENTER THE B VALUE");
b=Integer.parseInt(in.readLine());
System.out.println("ENTER THE C VALUE");
c=Integer.parseInt(in.readLine());
if(a>b)
if(a>c)
System.out.println("A IS BIG");
else
System.out.println("B IS BIG");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
else
if(b>a)
if(b>c)
System.out.println("B IS BIG");
else
System.out.println("C IS BIG");
else
if(c>a)
if(c>b)
System.out.println("C IS BIG");
else
System.out.println("THREE NOS ARE EQUAL");
}}}
Output
C:\Java\JDK16~1.0\bin>javac j7.java
Note: j7.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\Java\JDK16~1.0\bin>java j7
ENTER THE A VALUE
30
ENTER THE B VALUE
40
ENTER THE C VALUE
10
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
B IS BIG
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-8
//TO PRINT 0 TO 10 NUMBERS
class j8
public static void main(String args[])
for(int i=0;i<=10;i++)
System.out.println("THE OUTPUT IS\t"+i);
}}}
Output
C:\Java\JDK16~1.0\bin>java j8
THE OUTPUT IS 0
THE OUTPUT IS 1
THE OUTPUT IS 2
THE OUTPUT IS 3
THE OUTPUT IS 4
THE OUTPUT IS 5
THE OUTPUT IS 6
THE OUTPUT IS 7
THE OUTPUT IS 8
THE OUTPUT IS 9
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
THE OUTPUT IS 10
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-9
//TO PRINT 10 TO 0 NUMBERS
class j9
public static void main(String args[])
for(int i=10;i>=0;i--)
System.out.println("THE OUTPUT IS\t"+i);
}}
Outputp
C:\Java\JDK16~1.0\bin>javac j9.java
C:\Java\JDK16~1.0\bin>java j9
THE OUTPUT IS 10
THE OUTPUT IS 9
THE OUTPUT IS 8
THE OUTPUT IS 7
THE OUTPUT IS 6
THE OUTPUT IS 5
THE OUTPUT IS 4
THE OUTPUT IS 3
THE OUTPUT IS 2
THE OUTPUT IS 1
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
THE OUTPUT IS 0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-10
//TO PRINT ODD(1,3,5) NUMBERS
class j10
public static void main(String args[])
for(int i=1;i<=10;i=i+2)
System.out.println("THE OUTPUT IS\t"+i);
}}}
Output
C:\Java\JDK16~1.0\bin>javac j10.java
C:\Java\JDK16~1.0\bin>java j10
THE OUTPUT IS 1
THE OUTPUT IS 3
THE OUTPUT IS 5
THE OUTPUT IS 7
THE OUTPUT IS 9
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-11
//TO PRINT EVEN(2,4,6) NUMBERS
class j11
public static void main(String args[])
for(int i=0;i<=10;i=i+2)
System.out.println("THE OUTPUT IS\t"+i);
Output
C:\Java\JDK16~1.0\bin>javac j11.java
C:\Java\JDK16~1.0\bin>java j11
THE OUTPUT IS 0
THE OUTPUT IS 2
THE OUTPUT IS 4
THE OUTPUT IS 6
THE OUTPUT IS 8
THE OUTPUT IS 10
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-12
/*Write a program to generate a Triangle.
eg:
22
333
4 4 4 4 and so on as per user given number */
import java.io.*;
class triangle
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int 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");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-13
import java.io.*;
class tri
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int n = Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+j+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output
C:\Java\JDK16~1.0\bin>javac tri.java
C:\Java\JDK16~1.0\bin>java tri
Enter nth value
1 2
1 2 3
1 2 3 4
1 2 3 4 5
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-14
import java.io.*;
class tri1
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int n = Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+n+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac tri1.java
C:\Java\JDK16~1.0\bin>java tri1
Enter nth value
5 5
5 5 5
5 5 5 5
5 5 5 5 5
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-15
import java.io.*;
class tri2
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int n = Integer.parseInt(in.readLine());
for(int i=10;i>=n;i--){
for(int j=1;j<=i;j++){
System.out.print(" "+j+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac tri2.java
Note: tri2.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\Java\JDK16~1.0\bin>java tri2
Enter nth value
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-16
import java.io.*;
class tri3
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int n = Integer.parseInt(in.readLine());
for(int i=10;i>=n;i--){
for(int j=1;j<=i;j++){
System.out.print(" "+i+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac tri3.java
C:\Java\JDK16~1.0\bin>java tri3
Enter nth value
10 10 10 10 10 10 10 10 10 10
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-17
import java.io.*;
class tri4
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int n = Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++){
for(int j=10;j>=i;j--){
System.out.print(" "+j+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac tri4.java
C:\Java\JDK16~1.0\bin>java tri4
Enter nth value
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2
10 9 8 7 6 5 4 3
10 9 8 7 6 5 4
10 9 8 7 6 5
10 9 8 7 6
10 9 8 7
10 9 8
10 9
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-18
import java.io.*;
class tri5
public static void main(String args[])throws IOException
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter nth value");
int n = Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++){
for(int j=10;j>=i;j--){
System.out.print(" "+i+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Ouput:
C:\Java\JDK16~1.0\bin>javac tri5.java
C:\Java\JDK16~1.0\bin>java tri5
Enter nth value
10
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6
7 7 7 7
8 8 8
9 9
10
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-19
/* Display Triangle as follow : BREAK DEMO.
23
456
7 8 9 10 ... N */
import java.io.*;
class tri6
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int c=0;
System.out.println("Enter the nth values");
int n = Integer.parseInt(in.readLine());
loop1: for(int i=1;i<=n;i++){
loop2: for(int j=1;j<=i;j++){
if(c!=n)
c++;
System.out.print(c+" ");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
else
break loop1;
System.out.print("\n");
Output:
C:\Java\JDK16~1.0\bin>javac tri6.java
C:\Java\JDK16~1.0\bin>java tri6
Enter the nth values
10
23
456
7 8 9 10
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-20
/* Display Triangle as follow
10
101
0 1 0 1 */
import java.io.*;
class tri7
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the nth value");
int n=Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(((i+j)%2)+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac tri7.java
C:\Java\JDK16~1.0\bin>java tri7
Enter the nth value
10
010
1010
01010
101010
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-21:
/* Display Triangle as follow
24
369
4 8 12 16 ... N (indicates no. of Rows) */
import java.io.*;
class tri8
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the nth value");
int n = Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print((i*j)+" ");
System.out.print("\n");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac tri8.java
C:\Java\JDK16~1.0\bin>java tri8
Enter the nth value
24
369
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-22
/*Write a program to find Fibonacci series of a given no.
Example :
Input - 8
Output - 1 1 2 3 5 8 13 21
*/
import java.io.*;
class fibo
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the no for fibonacci series");
int num = Integer.parseInt(in.readLine());
System.out.println("*****Fibonacci Series*****");
int f1, f2=0, f3=1;
for(int i=1;i<=num;i++){
System.out.print(" "+f3+" ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-23
import java.io.*;
class swap
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the numbers for swapping");
int num1 = Integer.parseInt(in.readLine());
int num2 = Integer.parseInt(in.readLine());
System.out.println("\n***Before Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
//Swap logic
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("\n***After Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac swap.java
C:\Java\JDK16~1.0\bin>java swap
Enter the numbers for swapping
300
400
***Before Swapping***
Number 1 : 300
Number 2 : 400
***After Swapping***
Number 1 : 400
Number 2 : 300
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-24
/* Write a program to convert given no. of days into months and days.
(Assume that each month is of 30 days)
Example :
Input - 69
Output - 69 days = 2 Month and 9 days */
import java.io.*;
class daymonth
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the input for find Month and Day");
int num = Integer.parseInt(in.readLine());
int days = num%30;
int month = num/30;
System.out.println(num+" days = "+month+" Month and "+days+"
days");
}}}
Output:
C:\Java\JDK16~1.0\bin>javac daymonth.java
C:\Java\JDK16~1.0\bin>java daymonth
Enter the input for find Month and Day
75
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
75 days = 2 Month and 15 days
Program-25
/*Write a program to find whether given no. is Armstrong or not.
Example :
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
import java.io.*;
class armstrong
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter no to find armstrong no or not");
int num = Integer.parseInt(in.readLine());
int n = num; //use to check at last time
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,3);
num = num / 10;
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
System.out.println(n+" is not a Armstrong Number");
}}
Output:
C:\Java\JDK16~1.0\bin>javac armstrong.java
C:\Java\JDK16~1.0\bin>java armstrong
Enter no to find armstrong no or not
153
153 is an Armstrong Number
C:\Java\JDK16~1.0\bin>java armstrong
Enter no to find armstrong no or not
50
50 is not a Armstrong Number
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-26
/* Write a program to Find whether number is Prime or Not. */
import java.io.*;
class prime
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the no");
int num = Integer.parseInt(in.readLine());
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
System.out.println(num+" is not a Prime Number");
flag = 1;
break;
} }
if(flag==0)
System.out.println(num+" is a Prime Number");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac prime.java
C:\Java\JDK16~1.0\bin>java prime
Enter the no
5 is a Prime Number
C:\Java\JDK16~1.0\bin>java prime
Enter the no
20
20 is not a Prime Number
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-27
/* Write a program to find whether no. is palindrome or not.
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
import java.io.*;
class Palindrome
public static void main(String args[])throws IOException
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the no to find palindrome or not");
int num = Integer.parseInt(in.readLine());
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
if(reverse == n)
System.out.println(n+" is a Palindrome Number");
else
System.out.println(n+" is not a Palindrome Number");
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
}}
output
C:\Java\JDK16~1.0\bin>javac Palindrome.java
C:\Java\JDK16~1.0\bin>java Palindrome
Enter the no to find palindrome or not
12521
12521 is a Palindrome Number
C:\Java\JDK16~1.0\bin>java Palindrome
Enter the no to find palindrome or not
12345
12345 is not a Palindrome Number
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-28
public class MathLibraryExample
public static void main(String[] args)
int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;
System.out.println("i is " + i);
System.out.println("j is " + j);
System.out.println("x is " + x);
System.out.println("y is " + y);
// The absolute value of a number is equal to
// the number if the number is positive or
// zero and equal to the negative of the number
// if the number is negative.
System.out.println("|" + i + "| is " + Math.abs(i));
System.out.println("|" + j + "| is " + Math.abs(j));
System.out.println("|" + x + "| is " + Math.abs(x));
System.out.println("|" + y + "| is " + Math.abs(y));
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
// Truncating and Rounding functions
// You can round off a floating point number
// to the nearest integer with round()
System.out.println(x + " is approximately " + Math.round(x));
System.out.println(y + " is approximately " + Math.round(y));
// The "ceiling" of a number is the
// smallest integer greater than or equal to
// the number. Every integer is its own
// ceiling.
System.out.println("The ceiling of " + i + " is " + Math.ceil(i));
System.out.println("The ceiling of " + j + " is " + Math.ceil(j));
System.out.println("The ceiling of " + x + " is " + Math.ceil(x));
System.out.println("The ceiling of " + y + " is " + Math.ceil(y));
// The "floor" of a number is the largest
// integer less than or equal to the number.
// Every integer is its own floor.
System.out.println("The floor of " + i + " is " + Math.floor(i));
System.out.println("The floor of " + j + " is " + Math.floor(j));
System.out.println("The floor of " + x + " is " + Math.floor(x));
System.out.println("The floor of " + y + " is " + Math.floor(y));
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
// Comparison operators
// min() returns the smaller of the two arguments you pass it
System.out.println("min(" + i + "," + j + ") is " + Math.min(i,j));
System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));
System.out.println("min(" + i + "," + x + ") is " + Math.min(i,x));
System.out.println("min(" + y + "," + j + ") is " + Math.min(y,j));
// There's a corresponding max() method
// that returns the larger of two numbers
System.out.println("max(" + i + "," + j + ") is " + Math.max(i,j));
System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));
System.out.println("max(" + i + "," + x + ") is " + Math.max(i,x));
System.out.println("max(" + y + "," + j + ") is " + Math.max(y,j));
// The Math library defines a couple
// of useful constants:
System.out.println("Pi is " + Math.PI);
System.out.println("e is " + Math.E);
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
// Trigonometric methods
// All arguments are given in radians
// Convert a 45 degree angle to radians
double angle = 45.0 * 2.0 * Math.PI/360.0;
System.out.println("cos(" + angle + ") is " + Math.cos(angle));
System.out.println("sin(" + angle + ") is " + Math.sin(angle));
// Inverse Trigonometric methods
// All values are returned as radians
double value = 0.707;
System.out.println("acos(" + value + ") is " + Math.acos(value));
System.out.println("asin(" + value + ") is " + Math.asin(value));
System.out.println("atan(" + value + ") is " + Math.atan(value));
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
// Exponential and Logarithmic Methods
// exp(a) returns e (2.71828...) raised
// to the power of a.
System.out.println("exp(1.0) is " + Math.exp(1.0));
System.out.println("exp(10.0) is " + Math.exp(10.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));
// log(a) returns the natural
// logarithm (base e) of a.
System.out.println("log(1.0) is " + Math.log(1.0));
System.out.println("log(10.0) is " + Math.log(10.0));
System.out.println("log(Math.E) is " + Math.log(Math.E));
// pow(x, y) returns the x raised
// to the yth power.
System.out.println("pow(2.0, 2.0) is " + Math.pow(2.0,2.0));
System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0,3.5));
System.out.println("pow(8, -1) is " + Math.pow(8,-1));
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
// sqrt(x) returns the square root of x.
for (i=0; i < 10; i++) {
System.out.println( "The square root of " + i + " is " + Math.sqrt(i));
// Finally there's one Random method
// that returns a pseudo-random number
// between 0.0 and 1.0;
System.out.println("Here's one random number: " + Math.random());
System.out.println("Here's another random number: " + Math.random());
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac MathLibraryExample.java
C:\Java\JDK16~1.0\bin>java MathLibraryExample
i is 7
j is -9
x is 72.3
y is 0.34
|7| is 7
|-9| is 9
|72.3| is 72.3
|0.34| is 0.34
72.3 is approximately 72
0.34 is approximately 0
The ceiling of 7 is 7.0
The ceiling of -9 is -9.0
The ceiling of 72.3 is 73.0
The ceiling of 0.34 is 1.0
The floor of 7 is 7.0
The floor of -9 is -9.0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
The floor of 72.3 is 72.0
The floor of 0.34 is 0.0
min(7,-9) is -9
min(72.3,0.34) is 0.34
min(7,72.3) is 7.0
min(0.34,-9) is -9.0
max(7,-9) is 7
max(72.3,0.34) is 72.3
max(7,72.3) is 72.3
max(0.34,-9) is 0.34
Pi is 3.141592653589793
e is 2.718281828459045
cos(0.7853981633974483) is 0.7071067811865476
sin(0.7853981633974483) is 0.7071067811865475
acos(0.707) is 0.7855491633997437
asin(0.707) is 0.785247163395153
atan(0.707) is 0.6154085176292563
exp(1.0) is 2.7182818284590455
exp(10.0) is 22026.465794806718
exp(0.0) is 1.0
log(1.0) is 0.0
log(10.0) is 2.302585092994046
log(Math.E) is 1.0
pow(2.0, 2.0) is 4.0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
pow(10.0, 3.5) is 3162.2776601683795
pow(8, -1) is 0.125
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
The square root of 0 is 0.0
The square root of 1 is 1.0
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0
The square root of 5 is 2.23606797749979
The square root of 6 is 2.449489742783178
The square root of 7 is 2.6457513110645907
The square root of 8 is 2.8284271247461903
The square root of 9 is 3.0
Here's one random number: 0.10264548531371676
Here's another random number: 0.34382138928992967
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-29
import java.io.*;
class hormonic
public static void main(String args[])
float s=0f;
int n=0;
try
//DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter nth value");
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
n=Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++)
s+=(float)1/i;
System.out.println("sum="+s);
catch(IOException e)
System.out.println(e);
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
}
Output:
C:\Java\JDK16~1.0\bin>javac hormonic.java
C:\Java\JDK16~1.0\bin>java hormonic
Enter nth value
10
sum=2.9289684
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
//to find the perfect number
Program-30
import java.io.*;
class perfectno
public static void main(String args[])throws IOException
int s,n;
System.out.println("enter the nth value:");
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
n=Integer.parseInt(in.readLine());
System.out.println("List");
for(int i=100;i<n;i++)
s=0;
for(int j=1;j<=i/2;j++)
int l=i%j;
if(l==0)
s+=j;
if(s==i)
System.out.print(+i);
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
}}}
Output:
C:\Java\JDK16~1.0\bin>javac perfectno.java
C:\Java\JDK16~1.0\bin>java perfectno
enter the nth value:
1000
List
496
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
//to find sum&average of first N natural no's using do-while-forloop
Program-31
import java.io.*;
class sumavg
public static void main(String args[])throws IOException
int opt=0,n=0,s=0,i=1;
try
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.println("Enter N value");
n=Integer.parseInt(in.readLine());
System.out.println("main menu");
System.out.println("------------");
System.out.println("1.Using while loop");
System.out.println("2.Using dowhile loop");
System.out.println("3.Using for loop");
System.out.println("select the option<1,2,3>");
opt=Integer.parseInt(in.readLine());
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
switch(opt)
case 1: System.out.println("Using while loop");
while(i<=n)
s+=i++;
break;
case 2: System.out.println("Using do-while loop");
do
s+=i++;
while(i<=n);
break;
case 3: System.out.println("Using For loop");
for(i=1;i<=n;i++)
s+=i;
break;
default:
System.out.println("Your Option is Wrong");
break;
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
System.out.println("Sum="+s);
System.out.println("Average="+s/n);
catch(IOException e)
System.out.println(e);
}}}
Output:
C:\Java\JDK16~1.0\bin>javac sumavg.java
C:\Java\JDK16~1.0\bin>java sumavg
Enter N value
main menu
------------
1.Using while loop
2.Using dowhile loop
3.Using for loop
select the option<1,2,3>
Using while loop
Sum=15
Average=3
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
//to print prime number between 1 to 100
Program-32
import java.io.*;
class primeno
public static void main(String args[])throws IOException
int r=1;
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader l=new BufferedReader(in);
System.out.println("The Prime List");
System.out.println("Enter nth no");
int n=Integer.parseInt(l.readLine());
for(int i=2;i<=n;i++)
for(int j=2;j<i/2;j++)
r=i%j;
if(r==0)
break;
if(r!=0)
System.out.print(+i+"");
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac primeno.java
C:\Java\JDK16~1.0\bin>java primeno
The Prime List
Enter nth no
100
23457111317192329313741434753596167717379838997
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
program-33
import java.io.*;
class fact
public static void main(String args[])throws IOException
int n;
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader l=new BufferedReader(in);
try
System.out.println("Enter a +ve integers");
n=Integer.parseInt(l.readLine());
int f=1;
for(int i=1;i<=n;i++)
f*=i;
System.out.println(+n+"!="+f);
catch(IOException e)
System.out.println(e);
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac fact.java
C:\Java\JDK16~1.0\bin>java fact
Enter a +ve integers
5!=120
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-34—MATRIX MUL--
import java.io.*;
class arrmat
public static void main(String ar[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int c[][]=new int[2][2];
int i,j,k;
System.out.println("FIRST MATRIX");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.println("ENTER THE VALUE OF "+i+j+":");
a[i][j]=Integer.parseInt(in.readLine());
}}
System.out.println("SECOND MATRIX");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
System.out.println("ENTER THE VALUE OF "+i+j+":");
b[i][j]=Integer.parseInt(in.readLine());
}}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
c[i][j]+=a[i][k]*b[k][j];
}}}
System.out.println("\t A-MATRIX \t B-MATRIX \t C-MATRIX \n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.print("\t"+a[i][j]);
for(j=0;j<2;j++)
System.out.print("\t"+b[i][j]);
for(j=0;j<2;j++)
System.out.print("\t"+c[i][j]);
System.out.print("\n");
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac arrmat.java
C:\Java\JDK16~1.0\bin>java arrmat
FIRST MATRIX
ENTER THE VALUE OF 00:
ENTER THE VALUE OF 01:
ENTER THE VALUE OF 10:
ENTER THE VALUE OF 11:
SECOND MATRIX
ENTER THE VALUE OF 00:
ENTER THE VALUE OF 01:
ENTER THE VALUE OF 10:
ENTER THE VALUE OF 11:
A-MATRIX B-MATRIX C-MATRIX
2 3 1 5 11 28
5 4 3 6 17 49
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-35:--MAT ADD
import java.io.*;
class arradd
public static void main(String ar[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int c[][]=new int[2][2];
int i,j,k;
System.out.println("FIRST MATRIX");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.println("ENTER THE VALUE OF "+i+j+":");
a[i][j]=Integer.parseInt(in.readLine());
}}
System.out.println("SECOND MATRIX");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
System.out.println("ENTER THE VALUE OF "+i+j+":");
b[i][j]=Integer.parseInt(in.readLine());
}}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}}
System.out.println("\t A-MATRIX \t B-MATRIX \t C-MATRIX \n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.print("\t"+a[i][j]);
for(j=0;j<2;j++)
System.out.print("\t"+b[i][j]);
for(j=0;j<2;j++)
System.out.print("\t"+c[i][j]);
System.out.print("\n");
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
FIRST MATRIX
ENTER THE VALUE OF 00:
ENTER THE VALUE OF 01:
ENTER THE VALUE OF 10:
ENTER THE VALUE OF 11:
SECOND MATRIX
ENTER THE VALUE OF 00:
ENTER THE VALUE OF 01:
ENTER THE VALUE OF 10:
ENTER THE VALUE OF 11:
A-MATRIX B-MATRIX C-MATRIX
5 2 4 6 9 8
7 6 1 4 8 10
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-36:--MAT MUL
import java.io.*;
class arrmul
public static void main(String ar[])throws IOException
DataInputStream in=new DataInputStream(System.in);
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int c[][]=new int[2][2];
int i,j,k;
System.out.println("FIRST MATRIX");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.println("ENTER THE VALUE OF "+i+j+":");
a[i][j]=Integer.parseInt(in.readLine());
}}
System.out.println("SECOND MATRIX");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
{
System.out.println("ENTER THE VALUE OF "+i+j+":");
b[i][j]=Integer.parseInt(in.readLine());
}}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]-b[i][j];
}}
System.out.println("\t A-MATRIX \t B-MATRIX \t C-MATRIX \n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.print("\t"+a[i][j]);
for(j=0;j<2;j++)
System.out.print("\t"+b[i][j]);
for(j=0;j<2;j++)
System.out.print("\t"+c[i][j]);
System.out.print("\n");
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
FIRST MATRIX
ENTER THE VALUE OF 00:
ENTER THE VALUE OF 01:
ENTER THE VALUE OF 10:
ENTER THE VALUE OF 11:
10
SECOND MATRIX
ENTER THE VALUE OF 00:
ENTER THE VALUE OF 01:
ENTER THE VALUE OF 10:
ENTER THE VALUE OF 11:
A-MATRIX B-MATRIX C-MATRIX
5 2 1 5 4 -3
6 10 4 2 2 8
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-37
import java.util.*;
class vec
public static void main(String args[])
Vector Vect=new Vector(5,2);//created vector
System.out.println("size:"+Vect.size());
System.out.println("capacity:"+Vect.capacity());
for(int i=1;i<6;i++)
Vect.addElement(new Integer(i));
System.out.println("size:"+Vect.size());
System.out.println("capacity:"+Vect.capacity());
Vect.addElement(new Double(173.58));
System.out.println("capacity:"+Vect.capacity());
System.out.println("size:"+Vect.size());
}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac vec.java
C:\Java\JDK16~1.0\bin>java vec
size:0
capacity:5
size:5
capacity:5
capacity:7
size:6
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
program-38
import java.util.*;
class vec2
public static void main(String args[])
Vector list=new Vector();
int length=args.length;
System.out.println(+length);
for(int i=0;i<length;i++)
list.addElement(args[i]);
list.insertElementAt("COBOL",2);
int size=list.size();
String listArray[]=new String[size];
list.copyInto(listArray);
System.out.println("List of Languages:");
for(int i=0;i<size;i++)
System.out.println(listArray[i]);
}}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac vec2.java
C:\Java\JDK16~1.0\bin>java vec2 c c++ vb oracle java tally
List of Languages:
c++
COBOL
vb
oracle
java
tally
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
program-39:
// Demonstrate various Vector operations.
import java.util.*;
class Vec3 {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector v = new Vector(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " +
v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " +
v.capacity());
v.addElement(new Double(5.45));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Float(9.4));
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
v.addElement(new Integer(10));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " +
(Integer)v.firstElement());
System.out.println("Last element: " +
(Integer)v.lastElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
// enumerate the elements in the vector.
Enumeration vEnum = v.elements();
System.out.println("\\nElements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac vec3.java
C:\Java\JDK16~1.0\bin>java vec3
Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.
\nElements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-40
/*
Copy Elements of ArrayList to Java Vector Example
This java example shows how to copy elements of Java ArrayList to Java
Vector using
copy method of Collections class.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class cea {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("4");
arrayList.add("2");
arrayList.add("5");
arrayList.add("3");
//create a Vector object
Vector v = new Vector();
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
//Add elements to Vector
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
v.add("G");
v.add("H");
/*
To copy elements of Java ArrayList to Java Vector use,
static void copy(List dstList, List sourceList) method of Collections class.
This method copies all elements of source list to destination list. After
copy index of the elements in both source and destination lists would be
identical. The destination list must be long enough to hold all copied
elements. If it islonger than that, the rest of the destination list's elments
would remain unaffected.
*/
System.out.println("Before copy, Vector Contains : " + v);
//copy all elements of ArrayList to Vector using copy method of
Collections class
Collections.copy(v,arrayList);
/*Please note that, If Vector is not long enough to hold all elements of
ArrayList, it throws IndexOutOfBoundsException.
*/ System.out.println("After Copy, Vector Contains : " + v);
}}
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Output:
C:\Java\JDK16~1.0\bin>javac cea.java
C:\Java\JDK16~1.0\bin>java cea
Before copy, Vector Contains : [A, B, D, E, F, G, H]
After Copy, Vector Contains : [1, 4, 2, 5, 3, G, H]
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-41:
/* Find maxmimum element of Java ArrayList Example
This java example shows how to find a maximum element of Java ArrayList
using max method of Collections class.
*/
import java.util.ArrayList;
import java.util.Collections;
public class maxarraylist {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add(new Integer("327482"));
arrayList.add(new Integer("13408"));
arrayList.add(new Integer("802348"));
arrayList.add(new Integer("345308"));
arrayList.add(new Integer("509324"));
/* To find maximum element of Java ArrayList use, static Object
max(Collection c) method of Collections class. This method returns the
maximum element of Java ArrayList according to its natural ordering.
*/
Object obj = Collections.max(arrayList);
System.out.println("Maximum Element of Java ArrayList is : " + obj);
}}
Output :
Maximum Element of Java ArrayList is : 802348
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Program-42
//Find maxmimum element of Java Vector Example
/* Find maxmimum element of Java Vector Example
This java example shows how to find a maximum element of Java Vector
using max method of Collections class.
*/ import java.util.Vector;
import java.util.Collections;
public class findmaxvec {
public static void main(String[] args) {
//create a Vector object
Vector v = new Vector();
//Add elements to Vector
v.add(new Double("324.4324"));
v.add(new Double("345.3532"));
v.add(new Double("342.342"));
v.add(new Double("357.349"));
v.add(new Double("23.32453"));
/* To find maximum element of Java Vector use,
static Object max(Collection c) method of Collections class.
This method returns the maximum element of Java Vector according to
its natural ordering.
*/
Object obj = Collections.max(v);
System.out.println("Maximum Element of Java Vector is : " + obj);
}} Output :
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
Maximum Element of Java Vector is : 357.349
Program-43
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL
JAVA PROGRAMS 201
0
BY V.VENKATARAMAN., MCA., MPHIL