You are on page 1of 23

PRACTICAL-1

Basic Java Programs

(1) PROGRAM TO DISPLAY HELLO WORLD!


PROGRAM: class p01 { public static void main(String args[]) { System.out.println("HELLO WORLD!"); } }

(2) PROGRAM TO DISPLAY HELLO WORLD! \n I AM LEARNING JAVA \n AND I LOVE IT.
PROGRAM: class p02 { public static void main(String args[]) { System.out.println(HELLO WORLD! \n I AM LEARNING JAVA \n AND I LOVE IT.); } }

(3) GIVEN RADIUS OF CIRCLE AS 5 UNITS. WRITE A PROGRAM TO CALCULATE AND DISPLAY RADIUS, DIAMETER, CIRCUMFERENCE AND AREA.
PROGRAM: class p03 { public static void main(String args[]) { float pi=3.146f; int r=5; float a,c; a=pi*r*r; c=2*pi*r; System.out.println("Radius Of Circle :"+" "+ r); System.out.println("Diameter Of Circle :"+" "+ 2*r); System.out.println("Area Of Circle :"+" "+ a); System.out.println("Circumference of Circle :"+" "+ c); } }

(4) GIVEN A=10, B=25 AND C=100. WRITE A PROGRAM TO FIND THE GREATEST NUMBER.
PROGRAM: class p04 { public static void main(String args[]) { int a=10,b=25,c=100; if(a>b) { if(a>c) { System.out.println("Greatest No is :"+a); } else { System.out.println("Greatest No is :"+c); } } else { if(b>c) { System.out.println("Greatest No is :"+b); } else { System.out.println("Greatest No is :"+c); } } } }

(5) PROGRAM TO CHECK GIVEN NUMBER IS PRIME OR NOT.


PROGRAM: import java.io.*; class p05 { public static void main(String args[])throws IOException { InputStreamReader indata = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(indata); String instring; int a,i,b; System.out.println("Enter Any Number:"); instring = stdin.readLine(); a = Integer.parseInt(instring); for (i=2;i<a;i++) { b = a%i; if(b==0) { System.out.println("Number is not Prime."); System.exit(0); } } System.out.println("Number is Prime."); } }

(6) PROGRAM TO REVERSE THE GIVEN NUMBER.

PROGRAM: import java.io.*; class p06 { public static void main(String args[])throws IOException { InputStreamReader indata = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(indata); String instring; int a,b,c,d,s; System.out.println("Enter Any Two Digit Number:"); instring = stdin.readLine(); a = Integer.parseInt(instring); b = a%10; c = a/10; d = b*10; s = c+d; System.out.println("Reverse of " + a + " is " + s); } }

(7) PROGRAM TO FIND THE SUM OF THE SERIES:

1 + x + x2 + x3 + ..... PROGRAM: class p07 { public static void main(String args[]) { long x=1,y=1,k,j; for(j=1;j<=10;j++) { for(k=1;k<=j;k++) { x=x*2; } y=y+x; x=1; } System.out.println("Sum :"+y); } }

(8) PROGRAM TO PRINT THE FOLLOWING TRIANGLE:

5 45 345 2345 12345 012345 PROGRAM: class p08 { public static void main(String args[]) { int i,j; for(i=5;i>=0;i--) { for(j=i;j<=5;j++) { System.out.print(j+" "); } System.out.print("\n"); } } }

(9) PROGRAM TO PRINT THE FOLLOWING TRIANGLE:


1 12 123 1234 12345 PROGRAM: class p09 { public static void main(String args[]) { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { System.out.print(j+" "); } System.out.print("\n"); } } }

(10) PROGRAM TO DISPLAY VARIOUS PRIMITIVE TYPES.


PROGRAM: class p10 { public static void main(String args[]) { boolean b=true; char c=88; //code for X or x. byte j=127; short k=32767; int m=2147483647; long n=9223372036854775807L; float p=3.14159265f; double pi=3.141592653589793238; System.out.println("b =" + b); System.out.println("c =" + c); System.out.println("j =" + j); System.out.println("k =" + k); System.out.println("m =" + m); System.out.println("n =" + n); System.out.println("p =" + p); System.out.println("pi =" + pi); } }

PRACTICAL-2
Programs related to Input and Output
(11) PROGRAM TO READ CHARACTER STREAM FROM KEYBOARD AND DISPLAY IT ON THE SCREEN.
PROGRAM: import java.io.* ; class p11 { public static void main(String[] args)throws IOException { InputStreamReader instream = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(instream); String indata; System.out.println("Enter the string:"); indata = stdin.readLine(); System.out.println("Entered string is:"+ indata); } }

(12) PROGRAM TO READ INTEGER INPUT, CALCULATE ITS SQUARE AND


DISPLAY IT ON THE SCREEN. PROGRAM: import java.io.* ; class p12 { public static void main(String[] args)throws IOException { InputStreamReader instream = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(instream); String indata; int num,square; System.out.println("Enter any number:"); indata = stdin.readLine(); num = Integer.parseInt(indata); square = num*num; System.out.println("Square Value of " + num + " is " + square); } }

(13) PROGRAM TO READ 2 INTEGER INPUTS, CALCULATE THEIR SUM AND


DISPLAY IT ON THE SCREEN. PROGRAM: import java.io.* ; class p13 { public static void main(String[] args)throws IOException { InputStreamReader instream = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(instream); String indata1,indata2; int num1,num2,sum; System.out.println("Enter First number:"); indata1 = stdin.readLine(); num1 = Integer.parseInt(indata1); System.out.println("Enter Second number:"); indata2 = stdin.readLine(); num2 = Integer.parseInt(indata2); sum = num1+num2; System.out.println("Sum of " + num1 + " & " + num2 + " is " + sum); } }

(14) PROGRAM TO CONVERT STRING TO DOUBLE. PROGRAM: import java.io.* ; class p14 { public static void main(String[] args)throws IOException { InputStreamReader instream = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(instream); String indata; double num; System.out.println("Enter any number:"); indata = stdin.readLine(); num = Double.parseDouble(indata); System.out.println("Entered number in double is " + num); } }

(15) PROGRAM TO ASK USER FOR PRINCIPAL, RATE AND TIME(DOUBLE). THEN CALCULATE SIMPLE INTEREST USING FORMULA: SI=PRT/100. PROGRAM: import java.io.* ; class p15 { public static void main(String[] args) throws IOException { InputStreamReader inString=new InputStreamReader(System.in); BufferedReader stdin= new BufferedReader(inString); String s1,s2,s3; Double p,r,t,i; System.out.println("Enter Principle : "); s1=stdin.readLine(); p=Double.parseDouble(s1); System.out.println("Enter Rate : "); s2=stdin.readLine(); r=Double.parseDouble(s2); System.out.println("Enter Time : "); s3=stdin.readLine(); t=Double.parseDouble(s3); i=(p*r*t)/100; System.out.println("Simple Interest for Principle = " +p+ " Rate = " +r+ " Time = " +t+ " is " +i+ "."); } }

PRACTICAL-3
Programs related to Array
(16) PROGRAM TO CREATE 1-D ARRAY OF INTEGER, INITIALIZE IT AND DISPLAY IT ON THE SCREEN.
PROGRAM: import java.io.* ; class p16 { public static void main(String[] args) { int[] array1={1,2,3,4,5}; for(int i=0; i<=4; i++) { System.out.println("Array1[ " + i + " ] has " + array1[i]); } } }

(17) PROGRAM TO READ AN ARRAY OF INTEGER FROM KEYBOARD AND


DISPLAY IT ON THE SCREEN. USE array.length & Array.getLength(). PROGRAM: import java.io.* ; import java.lang.reflect.Array; class p17 { public static void main(String[] args) throws IOException { int[] array = new int[5]; int data; InputStreamReader inString=new InputStreamReader(System.in); BufferedReader stdin= new BufferedReader(inString); for(int i=0; i<array.length; i++) { System.out.println("Enter an Integer"); data=Integer.parseInt(stdin.readLine()); array[i]=data; } for(int i=0; i<Array.getLength(array); i++) { System.out.println("Array[ " + i + " ] = " + array[i]); } } }

(18) PROGRAM TO PASS ARRAY AS A PARAMETER.


PROGRAM: import java.io.*; class Arrayops { void print(int[] x) { for(int i=0;i<x.length;i++) System.out.println(x[i] + " "); System.out.println(); } } class p18 { public static void main(String args[]) { Arrayops operate = new Arrayops(); int[] vr = {30,-9,-8,37,41,45}; System.out.print("\n The array is:"); operate.print(vr); } }

(19) PROGRAM TO SORT THE ARRAY IN ASCENDING ORDER.

PROGRAM: import java.io.* ; import java.lang.reflect.Array; class p19 { public static void main(String args[]) throws IOException { int[] vr = new int[4]; int data,i,b,j; BufferedReader indata = new BufferedReader(new InputStreamReader(System.in)); for(i=0;i<vr.length;i++) { System.out.println("Enter an integer:"); data = Integer.parseInt(indata.readLine()); vr[i] = data; } for(i=0;i<vr.length;i++) { for(j=i+1;j<vr.length;j++) { b = vr[j]; if(vr[i]>=vr[j]) { vr[j] = vr[i]; vr[i] = b; } } } for(i=0;i<Array.getLength(vr);i++) { System.out.println("vr[" + i + "]=" + vr[i]); } } }

(20) PROGRAM FOR MULTIPLICATION OF 3x3 MATRICES.


PROGRAM: import java.io.* ; class p20 { public static void main(String args[])throws IOException { int[][] a = new int[3][3]; int[][] b = new int[3][3]; int[][] c = new int[3][3]; int d,e=0,i,j,k; BufferedReader indata = new BufferedReader(new InputStreamReader(System.in)); for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.println("Enter An Integer for a[" + i + "][" + j + "]:"); a[i][j] = Integer.parseInt(indata.readLine()); System.out.println("Enter An Integer for b[" + i + "][" + j + "]:"); b[i][j] = Integer.parseInt(indata.readLine()); } } for(i=0;i<3;i++) { for(k=0;k<3;k++) { for(j=0;j<3;j++) { d=a[i][j]*b[j][k]; e=e+d; } System.out.println("c[" + i + "][" + k + "] = " + e); e=0; } } } }

PRACTICAL-6
Programs related to branching and looping
(29) PROGRAM TO FIND THAT THE NUMBER THE USER ENTERS IS POSITIVE OR NEGATIVE. USE if-else. PROGRAM: import java.io.* ; class p29 { public static void main(String[] args) throws IOException { InputStreamReader inString=new InputStreamReader(System.in); BufferedReader stdin= new BufferedReader(inString); String s1; Integer p; System.out.println("Enter Number : "); s1=stdin.readLine(); p=Integer.parseInt(s1); if(p>=0) System.out.println("Number "+ p +" is positive"); else System.out.println("Number "+ p +" is negative"); } }

(30) PROGRAM THAT WILL SUM EACH INTEGER THE USER ENTERS.IN
THIS PROGRAM, WE USE A SPECIAL VALUE(sentinel) TO TELL THE PROGRAM THE LOOP IS DONE. i.e. USERS WILL ENTER A ZERO TO TELL THE PROGRAM THAT SUM IS COMPLETE. PROGRAM: class p30 { public static void main(String[] args) { int a=0,p; for(int i=0; i<=args.length; i++) { p=Integer.parseInt(args[i]); if(p==0) { break; } else { a=a+p; } } System.out.println("Sum is :" + a); } }

(31) A CLOTHING STORE MIGHT OFFER A DISCOUNT THAT DEPENDS ON


THE QUALITY OF THE GOODS: CLASS A GOODS ARE NOT DISCOUNTED AT ALL. CLASS B GOODS ARE DISCOUNTED 10%. CLASS C GOODS ARE DISCOUNTED 20%. ANYTHING ELSE IS DISCOUNTED 30%. WRITE A PROGRAM USING switch() & break() TO CALCULATE THE DISCOUNT. PROGRAM:
class switching { public static void main(String args[]) { double discount; char code; System.out.println("select the grade"); System.out.println("class A ---- no discount"); System.out.println("class B ---- 10% discount"); System.out.println("class C ---- 20% discount"); System.out.println("Anything else ---- 30% discount"); System.out.print("select the code------>"); System.out.flush(); try { switch ( code=(char)System.in.read()) { case 'A': discount = 0%; System.out.println(discount); break; case 'B': discount = 10%; System.out.println(discount); break; case 'C': discount = 20%; System.out.println(discount); break; default: discount = 30%; System.out.println(discount); } } catch(Exception e) { } } }

You might also like