0% found this document useful (0 votes)
1 views22 pages

Java Lab Record

Uploaded by

soulsiddharth074
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views22 pages

Java Lab Record

Uploaded by

soulsiddharth074
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming Lab Record - Complete

Experiment 1: Read Number from Standard Input


Aim:​
To read a number from standard input and display it.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
import [Link].*;​
public class Exp1{​
public static void main(String[] args){​
Scanner sc=new Scanner([Link]);​
int n=[Link]();​
[Link]("Number = "+n);​
}​
}

Sample Input:​
Input: 10

Sample Output:​
Number = 10

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 2: Get Input from User
Aim:​
To read a name and display it.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
import [Link].*;​
public class Exp2{​
public static void main(String[] args){​
Scanner sc=new Scanner([Link]);​
String name=[Link]();​
[Link]("Hello "+name);​
}​
}

Sample Input:​
Input: Ravi

Sample Output:​
Hello Ravi

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 3: Multiply Two Floating Point Numbers
Aim:​
To multiply two floating-point numbers.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp3{​
public static void main(String[] args){​
float a=2.5f,b=4.0f;​
[Link](a*b);​
}​
}

Sample Input:​
-

Sample Output:​
10.0

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 4: Swap Two Numbers
Aim:​
To swap two numbers.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp4{​
public static void main(String[] args){​
int a=10,b=20,t;​
t=a;a=b;b=t;​
[Link](a+" "+b);​
}​
}

Sample Input:​
-

Sample Output:​
20 10

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 5: Add Two Binary Strings
Aim:​
To add two binary strings.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp5{​
public static void main(String[] args){​
String a="1010",b="110";​
int x=[Link](a,2);​
int y=[Link](b,2);​
[Link]([Link](x+y));​
}​
}

Sample Input:​
-

Sample Output:​
10000

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 6: Add Two Complex Numbers
Aim:​
To add two complex numbers.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp6{​
public static void main(String[] args){​
int a=2,b=3,c=4,d=5;​
[Link]((a+c)+" + "+(b+d)+"i");​
}​
}

Sample Input:​
-

Sample Output:​
6 + 8i

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 7: Check Even or Odd
Aim:​
To check whether a number is even or odd.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp7{​
public static void main(String[] args){​
int n=7;​
[Link](n%2==0?"Even":"Odd");​
}​
}

Sample Input:​
-

Sample Output:​
Odd

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 8: Find Largest Among 3 Numbers
Aim:​
To find largest among three numbers.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp8{​
public static void main(String[] args){​
int a=10,b=25,c=15;​
[Link]([Link](a,[Link](b,c)));​
}​
}

Sample Input:​
-

Sample Output:​
25

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 9: Find LCM
Aim:​
To find LCM of two numbers.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp9{​
public static void main(String[] args){​
int a=12,b=18,l=[Link](a,b);​
while(l%a!=0||l%b!=0) l++;​
[Link](l);​
}​
}

Sample Input:​
-

Sample Output:​
36

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 10: Find GCD/HCF
Aim:​
To find GCD of two numbers.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp10{​
public static void main(String[] args){​
int a=12,b=18;​
while(b!=0){int t=b;b=a%b;a=t;}​
[Link](a);​
}​
}

Sample Input:​
-

Sample Output:​
6

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 11: Prime Numbers 1 to N
Aim:​
To display prime numbers from 1 to N.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp11{​
public static void main(String[] args){​
int n=20;​
for(int i=2;i<=n;i++){​
int f=0;​
for(int j=2;j<=i/2;j++) if(i%j==0) f=1;​
if(f==0) [Link](i+" ");​
}​
}​
}

Sample Input:​
N=20

Sample Output:​
2 3 5 7 11 13 17 19

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 12: Check Leap Year
Aim:​
To check leap year.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp12{​
public static void main(String[] args){​
int y=2024;​
[Link]((y%400==0||(y%4==0&&y%100!=0))?"Leap Year":"Not Leap Year");​
}​
}

Sample Input:​
2024

Sample Output:​
Leap Year

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 13: Armstrong Number
Aim:​
To check Armstrong numbers in a range.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp13{​
public static void main(String[] args){​
int n=153,s=0,t=n;​
while(t>0){int r=t%10;s+=r*r*r;t/=10;}​
[Link](s==n?"Armstrong":"Not Armstrong");​
}​
}

Sample Input:​
153

Sample Output:​
Armstrong

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 14: Neon Number
Aim:​
To check Neon number.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp14{​
public static void main(String[] args){​
int n=9,s=0,sq=n*n;​
while(sq>0){s+=sq%10;sq/=10;}​
[Link](s==n?"Neon":"Not Neon");​
}​
}

Sample Input:​
9

Sample Output:​
Neon

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 15: Vowel or Consonant
Aim:​
To check vowel or consonant.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp15{​
public static void main(String[] args){​
char c='A';​
[Link]("AEIOUaeiou".indexOf(c)>=0?"Vowel":"Consonant");​
}​
}

Sample Input:​
A

Sample Output:​
Vowel

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 16: Factorial
Aim:​
To find factorial.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp16{​
public static void main(String[] args){​
int n=5,f=1;​
for(int i=1;i<=n;i++) f*=i;​
[Link](f);​
}​
}

Sample Input:​
5

Sample Output:​
120

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 17: Even Sum of Fibonacci Series
Aim:​
To find sum of even Fibonacci terms.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp17{​
public static void main(String[] args){​
int a=0,b=1,sum=0,n=50;​
while(b<=n){ if(b%2==0) sum+=b; int t=a+b;a=b;b=t;}​
[Link](sum);​
}​
}

Sample Input:​
50

Sample Output:​
44

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 18: Simple Interest
Aim:​
To calculate simple interest.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp18{​
public static void main(String[] args){​
double p=1000,r=5,t=2;​
[Link]((p*r*t)/100);​
}​
}

Sample Input:​
P=1000,R=5,T=2

Sample Output:​
100.0

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 19: Compound Interest
Aim:​
To calculate compound interest.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp19{​
public static void main(String[] args){​
double p=1000,r=10,t=2;​
[Link](p*[Link](1+r/100,t)-p);​
}​
}

Sample Input:​
P=1000,R=10,T=2

Sample Output:​
210.0

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 20: Perimeter of Rectangle
Aim:​
To calculate perimeter of rectangle.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
public class Exp20{​
public static void main(String[] args){​
int l=10,b=5;​
[Link](2*(l+b));​
}​
}

Sample Input:​
L=10,B=5

Sample Output:​
30

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 21: Hello World Applet
Aim:​
To create Hello World applet.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
import [Link].*;import [Link].*;​
public class Exp21 extends Applet{​
public void paint(Graphics g){​
[Link]("Hello World",50,50);​
}​
}

Sample Input:​
-

Sample Output:​
Hello World

Result:​
Thus the program was executed successfully and the expected output was obtained.
Experiment 22: Simple Banner Applet
Aim:​
To create banner applet.

Procedure:

1. Open IDE.

2. Create Java Project.

3. Create class/applet.

4. Type and save code.

5. Compile.

6. Run.

7. Verify output.

Program:​
import [Link].*;import [Link].*;​
public class Exp22 extends Applet{​
public void paint(Graphics g){​
[Link]("WELCOME TO JAVA",50,50);​
}​
}

Sample Input:​
-

Sample Output:​
WELCOME TO JAVA

Result:​
Thus the program was executed successfully and the expected output was obtained.

You might also like