Astha Java
Astha Java
Object:- Write a java program to find the Fibonacci series using recursive and non
recursive functions.
Program:-
class fib {
int a,b,c;
void nonrecursive(int n) //Non recursive function to find the Fibonacci series.
{
a =0;
b=1;
System.out.print(a+ ", " + b);
c =a+b;
while(c<=n)
{
System.out.print(", " +c);
a =b;
b=c;
c=a+b;
}
}
int recursive(int n) // Recursive function to find the Fibonacci series.
{
if(n==0)
return (0);
if(n==1)
return (1);
else
return(recursive(n-1)+recursive(n-2));
}
}
int F1=f.recursive(i);
System.out.print(F1 + ", ");
}
}
}
Output:-
Program:-
Output:-
Program:-
Method overloading:-
import java.io.*;
class MethodOverloading {
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
System.out.println("add() with 2 parameters");
System.out.println(add(7, 3));
Student(){
System.out.println("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
Output:-
Program:-
// Employee Class
class Employee {
public String name;
public double salary;
// Manager Class
class Manager extends Employee {
@Override
public double calculateSalary() {
return salary * 0.25;
}
}
// Engineer Class
class Engineer extends Employee {
public Engineer(String name, double salary) {
super(name, salary);
}
@Override
8|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
public double calculateSalary() {
return salary * 0.5;
}
}
Output:-
Program:-
import java.io.*;
10 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
11 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –6
Program:-
//Multiply two matrices program in java
import java.io.*;
// Driver Class
class matricesmultiplication {
// Function to print Matrix
static void printMatrix(int M[][], int rowSize,
int colSize)
{
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
System.out.print(M[i][j] + " ");
System.out.println();
}
}
// Function to multiply
// two matrices A[][] and B[][]
static void multiplyMatrix(int row1, int col1,
int A[][], int row2,
int col2, int B[][])
{
int i, j, k;
System.out.println(
"\nMultiplication Not Possible");
return;
}
// Driver code
public static void main(String[] args)
{
int A[][] = { { 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 },
{ 4, 4, 4 } };
int B[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 } };
13 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
14 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –7
Object:-Write a Java program to display the Employee details using scanner class.
Program:-
class employeedetails
{
public static void main(String[] args) {
}
Output:-
15 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –8
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
16 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
17 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –9
Object:-Write a Java program to reverse an array.
Program:-
// Java Program to reverse an array
// using loop
import java.util.Arrays;
System.out.println("" + Arrays.toString(arr));
}
}
18 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
19 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –10
Object:-Write a Java program to checks whether a given number is ugly or not.
Program:-
// Java implementation to
// check if a number is ugly number
import java.io.*;
public class uglynumber {
// Condition to check if
// a number is divide by
// 2, 3, or 5
if (n % 2 == 0) {
return (isUgly(n / 2));
}
if (n % 3 == 0) {
return (isUgly(n / 3));
}
if (n % 5 == 0) {
return (isUgly(n / 5));
}
return 0;
}
// Driver Code
public static void main(String args[])
{
int no = isUgly(15);
if (no == 1)
System.out.println("Yes");
else
System.out.println("No");
}
}
20 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
21 | P a g e Roll No:2301200100062