Professional Documents
Culture Documents
(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); } } } }
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); } }
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); } }
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"); } } }
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); } }
(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]); } } }
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]); } } }
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); } }