0% found this document useful (0 votes)
2 views

Pooja Java File

The document contains a series of practical Java programming exercises, each demonstrating different programming concepts such as arithmetic operations, variable types, input methods, control structures, and methods. Each practical example includes code snippets and expected outputs, covering topics from basic 'Hello World' programs to more complex functionalities like factorial calculation and area computation using user-defined methods. The exercises are designed to help learners understand and apply various Java programming techniques.

Uploaded by

byhy92806
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Pooja Java File

The document contains a series of practical Java programming exercises, each demonstrating different programming concepts such as arithmetic operations, variable types, input methods, control structures, and methods. Each practical example includes code snippets and expected outputs, covering topics from basic 'Hello World' programs to more complex functionalities like factorial calculation and area computation using user-defined methods. The exercises are designed to help learners understand and apply various Java programming techniques.

Uploaded by

byhy92806
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

PRACTICAL NO.

1
1. W.A.P of the “Hello World” in java.
class First
{
public static void main(String[] args){
System.out.print("HELLO WORLD");
}
}

Output:
PRACTICAL NO. 2
2.W.A.P to perform Arithmetic operators in java.
 Addition.
 Subtraction.
 Multiplication.
 Division.
 Modulus.

1. Addition
class Add
{
public static void main(String arg[])
{
int a=26,b=15,c;
c=a+b;
System.out.print("Addtition of two numbers:"+c);
}
}

Output:
2. Subtraction
class Sub
{
public static void main(String arg[])
{
int a=26,b=15,c;
c=a-b;
System.out.print("Subtraction of two numbers:"+c);
}
}

Output:

3. Multiplication
class Multi
{
public static void main(String arg[])
{
int a=26,b=15,c;
c=a*b;
System.out.print("Multiplication of two numbers:"+c);
}
}

Output:
4. Division
class Division
{
public static void main(String[] args)
{
int a=2,b=40,c;
c=a/b;
System.out.print("DIVISION OF A AND B:"+c);
}
}

Output:

5. Modulus
class Modules
{
public static void main(String arg[])
{
int a=9,b=3,c;
c=a%b;
System.out.print("Modules of two numbers:"+c);
}
}

Output:
PRACTICAL NO.3
3.W.A.P to find factorial of a number in java.
class Factorial
{
public static void main(String arg[])
{
int n=5,i,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.print("The Factorial of the number is:"+fact);
}
}

Output:
PRACTICAL NO.4
4.W.A.P to print the value of a variables in java.
 LOCAL VARIABLE
class sam
{
public static void main(String arg[])
{
int a=20;
System.out.print(a);
}
}

Output:

 INSTANCE VARIABLE
class Ins
{
int a;
public static void main(String arg[])
{
Ins in=new Ins();
in.a=10;
Ins in1=new Ins();
in1.a=20;
System.out.print(in.a+""+in1.a);
}
}

Output:

 STATIC VARIABLE
class Static
{
static int a;
public static void main(String[] args)
{
Static a=new Static();
a.a=40;
Static ab=new Static();
ab.a=50;
System.out.println(a.a+ " "+ ab.a);
}
}

Output:
PRACTICAL NO.5
5. W.A.P to perform various input methods:
- nextFloat()
- nextLine()
- nextBoolean().

 nextFloat()
import java.util.Scanner;
class Fmul
{
public static void main(String args[])
{
float a,b,c;
System.out.print("Enter the value of a:");
Scanner obj= new Scanner(System.in);
a=obj.nextFloat();
System.out.print("Enter the value of b:");
b=obj.nextFloat();
c=a*b;
System.out.print("Multiplication of a and b:"+c);
}}
Output:

 nextLine().
import java.util.Scanner;
class Stringline
{
public static void main(String args[])
{
String a,b,c;
System.out.println("Enter your firstname:");
Scanner obj= new Scanner(System.in);
a=obj.nextLine();
System.out.println("Enter your nickname:");
b=obj.nextLine();
System.out.println("Enter your lastname:");
c=obj.nextLine();
System.out.println("My fullname is " +a+" "+c);
}}
Output:

 nextBoolean().
import java.util.Scanner;
class Boolean
{
public static void main(String args[])
{
boolean a;
System.out.println("Enter either true or false:");
Scanner obj=new Scanner(System.in);
a=obj.nextBoolean();
System.out.print(a);
}}
Output:

PRACTICAL NO.6
6. W.A.P to check whether the no is even or odd.
import java.util.Scanner;
class Even
{
public static void main(String args[])
{
int a;
System.out.println("Enter value of a:");
Scanner obj= new Scanner(System.in);
a=obj.nextInt();
if(a%2==0)
{
System.out.println("NUMBER IS EVEN.....");
}
else{
System.out.println("NUMBER IS ODD....");
}}}
Output:

PRACTICAL NO.7
7. W.A.P to perform arithmetic operators by using Scanner class.
import java.util.Scanner;
class Arithmetic
{
public static void main(String args[])
{
int a,b,c,d,e,f,g;
System.out.println("Enter value of a:");
Scanner S1=new Scanner(System.in);
a=S1.nextInt();
System.out.println("Enter value of b:");
b=S1.nextInt();
c=a+b;
System.out.println("Addition :"+c);
d=a-b;
System.out.println("Subtraction :"+d);
e=a*b;
System.out.println("Multplication:"+e);
f=a/b;
System.out.println("Division:"+f);
g=a%b;
System.out.println("Modulus:"+g);
}}

Output:
PRACTICAL NO.8
8. W.A.P to find greatest number by using relational operators.
import java.util.Scanner;
class Relational
{
public static void main(String args[])
{
int a,b;
System.out.println("Enter value of a:");
Scanner cin=new Scanner(System.in);
a=cin.nextInt();
System.out.println("Enter value of b:");
b=cin.nextInt();
if(a>b)
{
System.out.println("a is greater than b....");
}
else
{
System.out.println("b is greater than a....");
}
}}
Output:

PRACTICAL NO.9
9. W.A.P in java to perform the Logical operators.
- Logical and (&&)
- Logical or(||)
- Logical not(!)

 Logical and (&&)


class Land
{
public static void main(String arg[])
{
System.out.println((3>4)&&(4>3));
System.out.println((3>5)&&(3<5));
System.out.println((4<5)&&(5>3));
System.out.println((3>5)&&(4>5));
}}
Output:

 OR Logical operator.
class Lor
{
public static void main(String arg[])
{
System.out.println((3>5)||(3<5));
System.out.println((3>5)||(5>4));
System.out.println((4>5)||(2>5));
System.out.println((4>5)||(5>6));
}
}
Output:

 Logical NOT operator.


class Lnot
{
public static void main(String[] args)
{
int a = 10, b = 1;
System.out.println("!(a < b) = " + !(a < b));
System.out.println("!(a > b) = " + !(a > b));
}
}

Output:
PRACTICAL NO.10
10. W.A.P to perform the Assignment operator.
class Assign
{
public static void main(String[] args)
{
int a=10;
a+=5;
System.out.println("Resulting value of a is : "+a);
}
}
Output:

PRACTICAL NO.11
11. W.A.P to perform the instanceof operator.
class Inst
{
public static void main(String[] args)
{
Inst in =new Inst();
System.out.println(in instanceof Inst);
}
}
Output:

PRACTICAL NO.12
12. W.A.P to perform dot operator.
class Dot
{
int a;
public static void main(String args[])
{
Dot obj=new Dot();
obj.a=10;
System.out.print(obj.a);
}}
Output:

PRACTICAL NO.13
13.W.A.P in java to perform the bitwise operators.
- Bitwise (&), bitwise or (|).
- Bitwise XOR (^), Bitwise Compliment (~) operator.

 Bitwise (&), bitwise or (|).


import java.util.Scanner;
class Bandori
{
public static void main(String args[])
{
int a,b;
System.out.println("Enter value of a: ");
Scanner obj=new Scanner(System.in);
a= obj.nextInt();
System.out.println("Enter value of b: ");
b=obj.nextInt();
System.out.println("Bitwise AND: "+(a&b));
System.out.println("Bitwise OR: "+(a|b));
}}
Output:

 Bitwise XOR (^), Bitwise Compliment (~) operator.


class BCOMXOR
{
public static void main(String args[])
{
int a=10,b=20;
System.out.println("BITWISE COM:"+(~a));
System.out.println("BITWISE XOR:"+(a^b));
}}
Output:
PRACTICAL NO.14
14. W.A.P to perform conditional operator.
class co
{
public static void main(String args[])
{
int a=5,b=3,Max;
Max=a>b ? a:b;
System.out.print(Max);
}}
Output:
PRACTICAL NO.15
15. W.A.P to print any two tables by getting input from the user.
import java.util.Scanner;
class Table1
{
public static void main(String args[])
{
int a,b;
System.out.println("ENTER VALUE OF A:");
Scanner obj=new Scanner(System.in);
a=obj.nextInt();
System.out.println("ENTER VALUE OF B:");
b=obj.nextInt();
for(int i=1;i<=10;i++)
{
System.out.println(a+" * "+i+" = "+a*i);
}
for(int i=1;i<=10;i++)
{
System.out.println(b+" * "+i+" = "+b*i);}}}
Output:
PRACTICAL NO.16
16.W.A.P in java to check the result of a student using ladder if-else-if
statement.
import java.util.Scanner;
class Ladder
{
public static void main(String args[])
{
int marks;
System.out.print("Enter marks of student:");
Scanner m1=new Scanner(System.in);
marks=m1.nextInt();
if(marks>95)
{
System.out.print("Topper....");
}
else if(marks>90)
{
System.out.print("First....");
}
else if(marks>85)
{
System.out.print("second....");
}
else
{
System.out.print("Failed....");
}}}
Output:
PRACTICAL NO.17
17. W.A.P to find the greatest among 3 nos’ using nested if.
class Nestedif
{
public static void main(String args[])
{
int a=5,b=3,c=6;
if(a>b)
{
if(a>c)
{
System.out.print("a is greater than b and c:");
}
else{
System.out.print("c is greatest to b");
}}
else
if(b>c)
{
System.out.print("b is greatest to c");
}
else{
System.out.print("c is greatest to a");
}}}
Output:
PRACTICAL NO.18
18. W.A.P to implement factorial of number by using for loop.
class For
{
public static void main(String args[])
{
int n=5,i,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("FACTORIAL:"+fact);
}}

Output:
PRACTICAL NO.19
19.W.A.P to print the to print the n natural no (1-10).
class While
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}}}
Output:
PRACTICAL NO.20
20. W.A.P to implement break statement.
class Break
{
public static void main(String args[])
{
int i;
for(i=1;i<10;i++)
{
if(i==4)
{
break;
}
System.out.println(i);
}}}

Output:
PRACTICAL NO.21
21. W.A.P to implement continue statement.
class Continue
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
{
if(i==4)
{
continue;
}
System.out.println(i);
}}}
Output:
PRACTICAL NO.22
22. W.A.P to calculate area of circle, rectangle or triangle with user-defined
method.
import java.util.Scanner;
class Areacrt
{
public static void main(String args[])
{
int option;
Scanner obj=new Scanner(System.in);
System.out.println("Choose an option");
System.out.println(" 1 Circle");
System.out.println(" 2 Rectangle");
System.out.println(" 3 Triangle");
option=obj.nextInt();
switch(option)
{
case 1:
System.out.println("Enter a number");
Double radious=obj.nextDouble();
Double area=Math.PI*radious*radious;
System.out.println("Area of circle:"+area);
break;
case 2:
System.out.println("Enter length");
Double length=obj.nextDouble();
System.out.println("Enter the width");
Double width=obj.nextDouble();
double ar=length*width;
System.out.println("Area of rectangle:"+ar);
break;
case 3:
System.out.println("Enter the base");
Double base=obj.nextDouble();
System.out.println("Enter the height");
Double height=obj.nextDouble();
double hgt=0.5*base*height;
System.out.println("Area of rectangle:"+hgt);
break;
}}}
Output:
PRACTICAL NO.23
23. W.A.P to perform pass by value in method.
class Pvalue
{
public static void main(String args[])
{
int number=10;
System.out.println("BEFORE CALLING:"+number);
display(number);
System.out.println("AFTER CALLING:"+number);
}
public static void display(int num)
{
System.out.println("INSIDE FUNCTION:"+num);
num=25;
System.out.println("NEW VALUE OF NUM:"+num);
}}
Output:
PRACTICAL NO.24
24. W.A.P to perform pass by reference.
class Point {
int a, b;
public static void main(String args[])
{
Point P = new Point();
P.a = 10;
P.b = 20;
System.out.println(P.a + " " + P.b);
change(P);
System.out.println(P.a + " " + P.b);
}
public static void change(Point P1) {
P1.a = 30;
P1.b = 40;
System.out.println(P1.a + " " + P1.b);
}}
Output:
PRACTICAL NO.25
25. W.A.P to perform even/odd or factorial of a number using switch
statement.
import java.util.Scanner;
class Switch
{
public static void main(String args[])
{
int option;
Scanner obj=new Scanner(System.in);
System.out.println("choose an option");
System.out.println(" 1 check if the no. is odd or even");
System.out.println(" 2 calculate the factorial of no.");
option=obj.nextInt();
switch(option)
{
case 1:
System.out.println("enter a number");
int num1=obj.nextInt();
if(num1%2==0)
{
System.out.println("Number is even");
}
else
{
System.out.println("Number is odd");
}
break;
case 2:
System.out.println("Enter a number");
int num2=obj.nextInt();
int fact=1;
for(int i=1;i<=num2;i++)
{
fact*=i;
}
System.out.println("Factorial is:"+fact);
break;
default:
System.out.println("Invalid choice");
break;
}}}
Output:
PRACTICAL NO.26
26. W.A.P to perform the functionality of static and non-static method.
class A
{
int a=10;
static int b=20;
void show()
{
System.out.println("value of a and b:"+a+" "+b);
}
static void display()
{
System.out.println(b);
}}
class Demo
{
public static void main(String args[])
{
A a1=new A();
a1.show();
a1.display();
}}
Output:

You might also like