0% found this document useful (0 votes)
9 views20 pages

Java Practical File: 14 Programs Guide

Uploaded by

dddfrs35193
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)
9 views20 pages

Java Practical File: 14 Programs Guide

Uploaded by

dddfrs35193
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 PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |1

Instruction:
 Write java program on Lab manual
after database.
 Must Write every java program at
separate page and must mention
output.
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |2

1. Write a program in JAVA to print “Hello World”.


class Program1

public static void main(String [ ] args)

[Link](“Hello World”);

Output: Hello World


JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |3

2. Write a program in java to add two numbers.


class Program2

public static void main(String [ ] args)

int a=10,b=20;

[Link](a+b)

Output: 30
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |4

3. Write a program in java to find the greatest of two number.

class Program3
{
public static void main(String [ ] args)
{
int
a=10,b=20;
if(a>b)
[Link](a+” is greatest.”);
else
[Link](b+“ is greatest.”);
}
}

Output: 20 is greatest.
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |5

4. Write a program in java to check leap year.

class Program4

public static void main(String [ ] args)

int year=2020;

if(((year%4==0)&&(year%100!=0))||(year%400==0))

[Link](year+“ is Leap Year”);

Else

[Link](“Not a Leap year”);

Output: 2020 is Leap Year


JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |6

5. Write a program in java to check whether the number is odd or even.

class Program5

public static void main(String [ ] args)

int a=10;

if(a%2==0)

[Link](a+” is even”);

else

[Link](a+” is Odd”);

Output: 10 is even.
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |7

6. Write a program in java to print weekday using switch case.


class Program6

public static void main(String [ ] args)

int day=7;

switch(day

case 1:

[Link](“Monday);

break;

case 2:

[Link](“Tuesday”);

break;

case 3:

[Link](“Wednesday”);

break;
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |8

case 4:

[Link](“Thrusday”);

break;

case 5:

[Link](“Friday”);

break;

case 6:

[Link](“Saturday”);

break;

case 7:

[Link](“Sunday”);

break;

default

[Link](“Wrong input”);

Output: Sunday
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P ag e |9

7: Write a program in java to print square of number 1 to 5.

class Program7

{
public static void main(String [ ] args)
{

for(i=1;i<=5;i++)
{
[Link](”Square of ”+ i +”=”+ i*i);
}
}
}

Output:
Square of 1=1
Square of 2=4
Square of 3=9
Square of 4=16
Square of 5=25
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 10

8: Write a program in java to print table of 5.

class Program8
{
public static void main(String [ ] args)
{
int a=5;
for(int i=1;i<=10;i++)
{
[Link](a+”*”+i”=”a*i);
}
}
}
output:
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 11

9: write a program to print even number between numbers from 1 to

public class Proram9


{
public static void main (String[ ] args)
{
int number = 1;
while (number <= 10)
{
if(number%2==0)
{
[Link] ("Even No= " + number);
}
++number;
}
}
}

Output:

Even No = 2
Even No = 4
Even No = 6
Even No = 8
Even No = 10
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 12

10: Write a program in java to print number form 1 to 10.

class Program10
{
public static void main(String [ ] args)
{
int i=1;
while(i<=10
)
{
[Link](i);
i++;
}
}
}

Output:
1
2
3
4
5
6
7
8
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 13

9
10

11: Write a program in java to sort an array of String in


alphabetic order.
[Link];

class Program11
{
public static void main(String [ ] args)
{

String []

names={“Zubair”,”Nikita”,”yash”,”Amar”,”Bhanu”};

[Link](“Names Array before sorting:”);

for(int i=0; i<[Link]; i++)


{
[Link](names[i]+” , “);
}
[Link]()
;
[Link](names);
[Link](“Names Array after sorting:”);
for(int i=0;i<[Link];i++)
{
[Link](names[i]+” , “);
}
[Link]();
}
}
Output:
Names Array before sorting:
Zubair , Nikita , Yash , Amar , Bhanu
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 14

Names Array after sorting:


Amar ,Bhanu , Nikita , Yash ,Zubair

12: Write a program in java to illustrate the binary Search.

[Link];

[Link];

class Program12

public static void main(String [ ] args)


{

double [] marks={100,500,800,200,1000,300,600,900,400,700};

Scanner sc =new Scanner([Link]);

[Link](“Enter number to search=

”);int key=[Link]([Link]());

int

index=[Link](marks,key);

if(index!=-1)

[Link](“Element found at position “+ (index+1));

}
Else
[Link](“Element not found”);
}
}
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 15

Output:
Enter number to search=
500Element found at
position 2

13: Write a program in java to accept a String input and perform


concat(),length().

[Link].*;

class Program13
{
public static void main(String [ ] args)
{
String name=”Ashok”
[Link](“concat
‘Samrat’:”+[Link](“Samrat”);
[Link](“Length:”+[Link]());
}
}

Output:

concat ‘Samrat’: Ashok Smarat

Length: 5
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 16

14: write a program to print odd number between numbers from 1 to 10.

Solution:
public class WhileDemo
{
public static void main (String[ ] args)
{
int number = 1;
while (number <= 10)
{
if(number%2==1)
{
[Link] ("Odd no= " + number);
}
++number;
}
}
}

Output:

Odd no= 1
Odd no= 3
Odd no= 5
Odd no= 7
Odd no= 9
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 17

15: Write a program in java to find area of rectangle .

Solution:
class Program15
{
static double area(double length, double breadth)
{
return(length*breadth);
}

public static void main(String [ ] args)


{
double a=area(45.5,78.5);
[Link](“Area of rectangle= ”+a);
}
}

Output:
Area of rectangle= 3571.75
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 18

Ques 16: write java program to find area of rectangle by using class and object(object
oriented feature).

class Rectangle
{

int length; int width;

void insert(int I, int w)


{

length=l;

width=w;

void calculateArea()
{
[Link](length*width);
}

class TestRectangle1
{

public static void main(String [] args)


{

Rectangle r1=new Rectangle();

[Link](11,5);

[Link]();

}
Output: 55
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 19

Ques 17: write java program to find area of circle by using object oriented feature.

class Circle
{

int areaCircle(int radius)


{

return((22/7)*radius*radius);

class TestCircle
{

public static void main(String [] args)


{

Circle c1=new Circle();

int area=[Link](7);

[Link](area);
}

Output: 154
JAVA PRACTICAL FILE (TOTAL 14 PROGRAM) P a g e | 20

Ques 18: write java program to find perimeter of square by using object oriented
feature.
class Square
{

double perimeter (double side)


{

return(4*side);

class TestClass
{

public static void main(String [] args)


{

Square a1=new Square ();

double perim=a1. perimeter (7);

[Link](perim);
}

Output: 28

You might also like