You are on page 1of 14

Practical No: 01

AIM: Write a program to display two messages in seperate lines.

1 Solution
Practical1.java
class practical1
{
public static void main(String args[])
{
System.out.println("Heloo there welcome to");
System.out.println("Object Oriented programming in java");
}

2 Output
Practical No: 02
AIM: Write a program to display Shastri said :'Sachin has played a game of his life'.

Solution
Practical2.java
class practical2
{
public static void main(String args[])
{
System.out.println("Shastri said :'Sachin has played a game of his
life'");
}

Output

\
Practical No: 03
AIM: Write a program to calculate fibonacci series.

3 Solution
fibonacci.java
/*Practical 2 program to write fibonacci series upto limit provided by user.
First attempt in commandline argument*/
class fibonacci
{
void create_fibonacci(int n)
{
int n1,n2,n3,i,sum;
n1 = 0;
n2 = 1;
i = 2;
while(i<n) /*As n1 and n2 are 0 and 1 so looping begins from 2*/
{
n3 = n1+n2;
System.out.print(n3+" ");
n1 = n2;
n2 = n3;
i++;
}

public static void main(String args[])


{
fibonacci f1 = new fibonacci();
System.out.println("Printing the fibonacci sequence until
15");
System.out.println(" ");
f1.create_fibonacci(15);
}

4 Output
Practical No: 04
AIM: Write a program to display addition of 2 numbers.

5 Solution
addition.java
class addition
{
int add(int x, int y)
{
return x+y;
}
public static void main(String args[])
{
addition A = new addition();
int sum = 0;
sum = A.add(34,567);
System.out.println("The sum of the numbers is "+sum);
}

6 Output
Practical No: 05
AIM: Write a program to display 2nd command line argument output.

7 Solution
addition.java
class CM_output
{
public static void main(String args[])
{
for(int i = 0; i<5; i++)
{
System.out.println("Printing the " +i + "th command line
data " +args[i] );
}
}

8 Output
Practical No: 06
AIM: Write a program to check if the number is even or odd by passing argument in
commandline argument.

9 Solution
addition.java
/*Using scanner class we invoke it's object as "in" in this program and utilize it
to invoke nextInt function
to take an integer input*/
import java.util.Scanner;
class evenodd
{
int even_odd(int n)
{
if(n%2==0)
return 1;
else
return -1;
}

public static void main(String args[])


{
evenodd e = new evenodd();
int result = 0;
int x = 0;
Scanner in = new Scanner(System.in);
x = in.nextInt();
System.out.println("Enter the number " +x);
result = e.even_odd(x);
if(result == 1)
System.out.println("Given number is even");
else
System.out.println("Given number is odd");
}
}

10 Output
Practical No: 07
AIM: Write a program in java to find the largest among three numbers (if else
statement).

11 Solution
addition.java
import java.util.Scanner;
class practical7
{
int MAX(int x, int y, int z)
{
if(x>y && x>z)
return x;
else if(y>x && y>z)
return y;
else
return z;
}
public static void main(String args[])
{
practical7 p = new practical7();
Scanner in = new Scanner(System.in);
int a,b,c,result;
a = b = c = 0;
System.out.println("Enter the three numbers in order to calulate the
largest among them");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
result = p.MAX(a,b,c);
System.out.println("The Largest among the three is "+result);
}
}

Output
Practical No: 08
AIM: Write a program to display factorial of number

Solution
factorial.java
class factorial
{
public static long create_factorial(long n)
{
if(n<=1)
return 1;
else
{
return n*create_factorial(n-1);
}
}

public static void main(String args[])


{
System.out.println("The factorial is "+ create_factorial(7));
}
}

OUTPUT:
Practical No: 09
AIM: Write a program to swap numbers using XOR operator

Solution
factorial.java
import java.util.Scanner;
public class SWAP
{
public static void main(String args[])
{
int num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number:");
num1 = scanner.nextInt();
System.out.print("Enter second number:");
num2 = scanner.nextInt();
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
scanner.close();
System.out.println("The First number after swapping:"+num1);
System.out.println("The Second number after swapping:"+num2);
}
}

OUTPUT:
Practical No: 10
AIM: Write a program to generate patterns

Solution
Pattern.java
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: "); //takes input from user

int rows = sc.nextInt();

for (int i= rows-1; i>=0 ; i--)


{
for (int j=0; j<=i; j++)
{
System.out.print("1" + " ");
}
System.out.println();
}
sc.close();
}
}

OUTPUT:

You might also like