PRACTICAL FILE
OF
JAVA LAB
Submitted To: Submitted By:
Sonal Arora. Jyoti Guar
Assistant Professor 6th Sem (ID-86053) C SE
Dept. BCA
Practical software Lab – Based on paper
BCA-307
INDEX
Sr. PRACTICAL DATE SIGNATURE
No.
1 WAP to implement overloading and
overriding in java.
2 WAP to implement all types of inheritance in
java.
3 WAP to implement use of finally keyword in
exception handling in java.
4 WAP to perform string operations in java.
5 WAP to implement thread
synchronization in java.
Solution: Using Recursion.
using System;
public class FibonacciRecursive
{
static void Main(string[] args)
{
[Link]("Please enter a number");
int number = Convert.ToInt32([Link]());
Fibonacci(0, 1, 1, number);
}
public static void Fibonacci(int a, int b, int counter, int number)
{
[Link](" " + a);
if (counter < number) Fibonacci(b, a + b, counter + 1, number);
}
}
OUTPUT
PROGRAM 2
Write a C# program to check prime number.
using System;
public class Prime
{
public static void Main(string[] args)
{
int n, i, f = 0;
[Link]("Enter the Number to check Prime: ");
n = [Link]([Link]());
int m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
[Link]("Number is not Prime.");
f = 1;
break;
}
}
if (f == 0)
{
[Link]("Number is Prime.");
}
[Link](); // To Hold Console Screen
}
}
OUTPUT
PROGRAM 3
Write a C# program to check palindrome number.
using System;
public class PalindromeNumber
{
public static void Main(string[] args)
{
int a, b, sum = 0, temporary;
[Link]("Enter the Number: ");
a = [Link]([Link]());
temporary = a;
while (a > 0)
{
b = a % 10;
sum = (sum * 10) + b;
a = a / 10;
}
if (temporary == sum)
[Link]("Number is Palindrome.");
else
[Link]("Number is not Palindrome");
}
}
OUTPUT
PROGRAM 4
Write a C# program to print factorial of a number.
using System;
public class FactorialExample
{
public static void Main(string[] args)
{
int i, fact = 1, number;
[Link]("Enter any Number: ");
number = [Link]([Link]());
for (i = 1; i <= number; i++)
{
fact = fact * i;
}
[Link]("Factorial of " + number + " is: " + fact);
[Link]();
}
}
OUTPUT
PROGRAM 5
Write a C# program to reverse given number.
using System;
public class PalindromeNumber
{
public static void Main(string[] args)
{
int a, b, sum = 0;
[Link]("Enter the Number: ");
a = [Link]([Link]());
while (a > 0)
{
b = a % 10;
sum = (sum * 10) + b;
a = a / 10;
}
[Link]("Reverse Number is " +sum);
[Link]();
}
}
OUTPUT
PROGRAM 6
Write a C# program to swap two numbers without using third variable.
using System;
public class SwapNumberProgram
{
public static void Main(string[] args)
{
int a, b;
[Link]("Enter First Number: ");
a = [Link]([Link]());
[Link]("Enter Second Number: ");
b = [Link]([Link]());
[Link](".......................................................
............");
[Link]("Before Swapping First Number = " + a + " And
Second Number = " + b);
a = a * b;
b = a / b;
a = a / b;
[Link](".......................................................
............");
[Link]("After Swapping First Number = " + a + " And
Second Number = " + b);
[Link]();
}
}
OUTPUT
PROGRAM 7
Write a C# program to explain Constructor.
using System;
public class Example
{
public Example()
{
[Link]("Default Constructor Called");
}
public static void Main(string[] args)
{
Example ex1 = new Example();
Example ex2 = new Example();
Example ex3 = new Example();
[Link]();
}
OUTPUT
PROGRAM 8
Write a C# program to explain function overloading.
using System;
namespace Functionoverloading
{
class shape
{
public void Area(int number)
{
int squarearea = number*number;
[Link]("The Area of Square is :" + squarearea);
}
public void Area(int l, int b)
{
int rectarea = l * b;
[Link]("The Area of Rectangle is :" + rectarea);
}
class Program
{
static void Main(string[] args)
{
shape s = new shape();
[Link](10);
[Link](10, 20);
[Link]();
}
}
}
OUTPUT
PROGRAM 9
Write a C# program to explain Inheritance.
using System;
public class Employee
{
public float salary = 35000;
}
public class Programmer: Employee
{
public float bonus = 15000;
}
class TestInheritance{
public static void Main(string[] args)
{
Programmer p1 = new Programmer();
[Link]("Salary: " + [Link]);
[Link]("Bonus: " + [Link]);
}
}
OUTPUT
PROGRAM 10
Write a C# program to explain Exception Handling.
using System;
class ExampleExceptionHandling
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100 / x;
[Link]("This line not executed");
}
catch (DivideByZeroException)
{
[Link]("Exception occured");
}
[Link]($"Result is {div}");
[Link]();
}
}
OUTPUT