Sei sulla pagina 1di 20

LAB EXERCISE UNIT: 1 1. Write a program to display any message. CODE: import java.io.

*; class Hello { public static void main(String args[]) { System.out.println("Hello Java"); } } OUTPUT: Hello Java.

2. Write a java program to display default value of all primitive data types of java. CODE: import java.io.*; class primitive { public static void main(String args[]) { byte a=2; short b=5; int c=6; long d=12345; float e=1; double f=2.34; char g='d'; Boolean h=true; System.out.println("Byte = "+a+", Short = "+b+", Integer = "+c+", Long = "+d+", Float = "+e+", Double = "+f+", Character = "+g+", Boolean = "+h); } } OUTPUT: Byte = 2, Short = 5, Integer = 6, Long = 12345, Float = 1.0, Double = 2.34, Char acter = d, Boolean = true

3. Write a program to check two strings are equal or not.

CODE: import java.io.*; class stringequal { public static void main(String args[]) throws IOException { String s1,s2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter string s1: "); s1=br.readLine(); System.out.println("Enter string s2: "); s2=br.readLine(); if(s1.equals(s2)) { System.out.println("Strings are equal"); } else { System.out.println("Strings are not equal"); } } } OUTPUT: Enter string s1: hello world Enter string s2: Hello world Strings are not equal

UNIT: 2

Write a program to give the examples of operators. 4. Increment and Decrement operators. CODE: import java.io.*; class inc { public static void main(String args[])

{ int i=2; System.out.println(+(++i)); System.out.println(+(--i)); } } OUTPUT: 3 2

5. Bitwise Complement operator. CODE: import java.io.*; class bitcomp { public static void main(String args[]) { int i=6; System.out.println("Before complementation: i = "+i); System.out.println("After complementation: i= "+(~i)); } } OUTPUT: Before complementation: i = 6 After complementation: i= -7

6. Arithmetic operators. CODE: import java.io.*; class arith { public static void main(String args[]) { int a,b; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); System.out.println("Addition = "+(a+b)+", Substraction = "+(a-b)+", Multiplication = "+(a*b)+", Division = "+(float)a/b+", Modulus (Remainder) = "+a%b); }

} OUTPUT: Addition = 10, Substraction = 4, Multiplication = 21, Division = 2.3333333, Modu lus (Remainder) = 1

7. Relational operators. CODE: import java.io.*; class relat { public static void main(String args[]) { int a,b; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); if(a>b) { System.out.println(a+" is greater than "+b); } else { System.out.println(a+" is less than "+b); } } } OUTPUT: 4 is less than 6

8. Bitwise operators. CODE: import java.io.*; class bitwise { public static void main(String args[]) { int a,b; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]);

System.out.println("Bitwise AND: "+(a&b)); System.out.println("Bitwise OR: "+(a|b)); } } OUTPUT: Bitwise AND: 0 Bitwise OR: 11

9. Conditional operators. CODE: import java.io.*; class conditional { public static void main(String args[]) { int a,b; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); if(a%2==0 && b%2==0) { System.out.println(a+" and "+b+" are both even"); } else { System.out.println(a+" and "+b+" both are not even"); } } } OUTPUT: 2 and 6 are both even

Write a program to give examples of control statements.

10. If statements. CODE: import java.io.*;

class ifstate { public static void main(String args[]) { int a,b; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); if(a==b) { System.out.println("a and b are equal"); } else { System.out.println("a and b are not equal"); } } } OUTPUT: a and b are not equal

11. Switch statements. CODE: import java.io.*; class switchstate { public static void main(String args[]) { char c='m'; switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u':System.out.println(c+" is a vowel"); break; default:System.out.println(c+" is a consonant"); } } }

OUTPUT: m is a consonant

12. For loop. CODE: import java.io.*; class forloop { public static void main(String args[]) { for(int i=0;i<5;i++) { System.out.println(i); } } } OUTPUT: 0 1 2 3 4

13. While statement. CODE: import java.io.*; class whileloop { public static void main(String args[]) { int a,fact=1; a=Integer.parseInt(args[0]); int b=a; while(a>=1) { fact*=a; a--; }

System.out.println("Factorial of "+b+" is "+fact); } } OUTPUT: Factorial of 6 is 720

14. Do-while statement. CODE: import java.io.*; class dowhileloop { public static void main(String args[]) { int a,fact=1; a=Integer.parseInt(args[0]); int b=a; do { fact*=a; a--; }while(a>=1); System.out.println("Factorial of "+b+" is "+fact); } } OUTPUT: Factorial of 4 is 24

Write a program to calculate the following: 15. Find the length of array. CODE: import java.io.*; class arraylen { public static void main(String args[]) { int a[]={1,2,3,4,5}; System.out.println("Length of array is: "+(a.length));

} } OUTPUT: Length of array is: 5

16. Demonstrate a one-dimensional array. CODE: import java.io.*; class array { public static void main(String args[]) { int a[]; a=new int[2]; a[0]=1; a[1]=2; System.out.println("Elements of the array are: "); for(int i=0;i<2;i++) { System.out.println(a[i]); } } } OUTPUT: 1 2

17. Demonstrate a two-dimensional array. CODE: import java.io.*; class array2 { public static void main(String args[]) { int a[][]; a=new int[2][3]; a[0][0]=1; a[0][1]=2;

a[0][2]=3; a[1][0]=4; a[1][1]=5; a[1][2]=6; System.out.println("Elements of the array are: "); for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { System.out.println(a[i][j]); } } } } OUTPUT: 1 2 3 4 5 6

18. Demonstrate a multi-dimensional array. CODE: import java.io.*; class multiarray { public static void main(String args[]) { int a[][][]; a=new int[1][2][3]; a[0][0][0]=1; a[0][0][1]=2; a[0][0][2]=3; a[0][1][0]=4; a[0][1][1]=5; a[0][1][2]=6; System.out.println("Elements of the array are: "); for(int i=0;i<1;i++) { for(int j=0;j<2;j++)

{ for(int k=0;k<3;k++) { System.out.println(a[i][j][k]); } } } } } OUTPUT: 1 2 3 4 5 6

Write a program to give example for command line arguments. 19. To get the name using command line. CODE: import java.io.*; class cmdline2 { public static void main(String args[]) throws IOException { String name; System.out.println("Enter any name: "); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); name=br.readLine(); System.out.println("Entered name is: "+name); } } OUTPUT: Enter any name: Sandeep Entered name is: Sandeep

Write a program to print the following triangle. 20. 1 101 10001 1000001 100000001 CODE: import java.io.*; class pattern { public static void main(String args[]) { for(int i=1;i<=9;i+=2) { for(int j=i;j<=i;j++) { System.out.print("1"); for(int k=1;k<=i-2;k++) { System.out.print("0"); } } if(i>1) System.out.print("1"); System.out.println(); } } } OUTPUT: 1 101 10001 1000001 100000001

21. 5 45 345 2345 12345

012345 CODE: import java.io.*; class pattern2 { public static void main(String args[]) { for(int i=5;i>=0;i--) { int k=i; for(int j=5;j>=i;j--) { System.out.print(k); k++; } System.out.println(); } } } OUTPUT: 5 45 345 2345 12345 012345 22. 1 12 123 1234 12345 CODE: import java.io.*; class pattern3 { public static void main(String args[]) { for(int i=1;i<=5;i++) {

for(int j=1;j<=i;j++) { System.out.print(j); } System.out.println(); } } } OUTPUT: 1 12 123 1234 12345

Write a program to find the following:

23. Prime number CODE: import java.io.*; class prime { public static void main(String args[]) { int i; int a=Integer.parseInt(args[0]); if(a<2) System.out.println(a+" is not a prime number"); for(i=2;i<=(a-1);i++) { if(a%i==0) { System.out.println(a+" is not a prime number"); break; } } if(a==i) System.out.println(a+" is a prime number"); } }

OUTPUT: 7 is a prime number

24. Sum of digit. CODE: import java.io.*; class sumofdigit { public static void main(String args[]) { int sum=0,a=Integer.parseInt(args[0]); int a1=a; for(int i=a;i>=1;i/=10) { a=i; a%=10; sum+=a; } System.out.println("Sum of digits of "+a1+" is: "+sum); } } OUTPUT: Sum of digits of 1238 is: 14

25. Write a program to arrange the numbers in ascending order. CODE: import java.io.*; class arrayascend { public static void main(String args[]) { int a[],i,j; a=new int[10]; for(i=0;i<10;i++) { a[i]=Integer.parseInt(args[i]);

} for(j=0;j<10;j++) { for(i=0;i<9;i++) { if(a[i+1]<a[i]) { a[i+1]+=a[i]; a[i]=a[i+1]-a[i]; a[i+1]-=a[i]; } } } System.out.println("Elements of the array in ascending order are: "); for(i=0;i<10;i++) { System.out.println(a[i]); } } } OUTPUT: Input elements: 1 2 -9 0 76 3 5 4 2 3 43 -9 0 1 2 3 4 5 23 43 76 26. Write a program to calculate the roots of quadratic equations. CODE: import java.io.*; class quadratic { public static void main(String args[]) {

double a=Integer.parseInt(args[0]),b=Integer.parseInt(args[1]),c=Integer.parseInt(args[2]),d,i; d=b*b-4*a*c; if(d==0) { System.out.println("Roots of the equation are: r1= "+-b/(2*a)+" r2= "+-b/(2*a)); } else if(d<0) { System.out.println("Roots are imaginary"); } else { for(i=1;i<=d;i+=0.001) { if(i*i>d) break; } System.out.println("Roots of the equation are: "); System.out.println("r1= "+(-b+i)/(2*a)+" r2= "+(-b-i)/(2*a)); } } } OUTPUT: For a=12, b=34, c=8 Roots of the equation are: r1= -0.25895833333287605 r2= -2.5743750000004573

27. Write a program for calculating matrix addition. CODE: import java.io.*; class matrixadd { public static void main(String args[]) { int a[][],b[][],k=0; a=new int[2][3]; b=new int[2][3];

for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { a[i][j]=Integer.parseInt(args[k]); k++; } } for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { b[i][j]=Integer.parseInt(args[k]); k++; } } System.out.println("Addition of the two matrices is: "); for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { System.out.println(a[i][j]+b[i][j]); } } } } OUTPUT: Matrix 1: 12 21 90 9 23 34 Matrix 2: 2 4 3 5 6 7 Addition of the two matrices is: 14 25 93 14 29 41

28. Write a program for calculating Matrix multiplication. CODE:

import java.io.*; class matrixmul { public static void main(String args[]) { int a[][],b[][],k=0; a=new int[2][3]; b=new int[3][2]; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { a[i][j]=Integer.parseInt(args[k]); k++; } } for(int i=0;i<3;i++) { for(int j=0;j<2;j++) { b[i][j]=Integer.parseInt(args[k]); k++; } } System.out.println("Matrix Multiplication is : "); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { for(k=0;k<1;k++) { System.out.println((a[i][k]*b[k][j])+(a[i][k+1]*b[k+1][j])+(a[i][k+2]*b[k+2][j])); } } } } } OUTPUT: Matrix 1: 12 23 34 45 9 8 Matrix 2: 7 6 1 2 3 4 Matrix Multiplication is :

209 254 348 320

Potrebbero piacerti anche