Lab Exercise – 1
Q1. Write a program in Java to print “Hello World”.
Ans:
class First
{
public static void main ( String args[ ])
{
[Link]("Hello World");
}
}
Output:
Q2: Write a program to print the following patterns:
Ans:
i)
class Main
{
public static void main (String args[])
{
int a = [Link](args[0]);
for (int i = 0; i < a; i++)
{
for (int j = 0; j < a - i; j++)
{
[Link](" ");
}
for (int k = 0; k <= i; k++)
{
[Link]("* ");
}
[Link]();
}
}
}
Output:
1
ii)
public class Main
{
public static void main (String args[])
{
int a = [Link](args[0]);
for (int i = a; i >= 0; i--)
{
for (int j = 0; j < a - i; j++)
{
[Link](" ");
}
for (int k = 0; k <= i; k++)
{
[Link]("* ");
}
[Link]();
}
}
}
Output:
Q3: Write a program in Java to print the table of a number received through command
line argument.
Ans:
class Main
{
public static void main(String[] args)
{
int a = [Link](args[0]);
for(int i = 1; i <= 10; i++)
[Link](a + " X " + i + " = " + a * i);
}
}
2
Lab Exercise – 2
Q1. Write a Java program to perform basic Calculator operations.
Ans:
import [Link];
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbrs: ");
double fir = [Link]();
double sec = [Link]();
[Link]("Enter an operator (+, -, *, /): ");
char oper = [Link]().charAt(0);
double res;
switch(oper)
{
case '+':
res = fir + sec;
break;
case '-':
res = fir - sec;
break;
case '*':
res = fir * sec;
break;
case '/':
res = fir / sec;
break;
default:
[Link]("Error! operator is not correct");
return;
}
[Link]("%.1f %c %.1f = %.1f", fir, oper, sec, res);
}
}
Q2: Write a Java program to calculate a Factorial of a number.
3
Ans:
import [Link];
class Factorial
{
public static void main(String args[])
{
int n, c, f = 1;
[Link]("Enter an integer to calculate its factorial");
Scanner in = new Scanner([Link]);
n = [Link]();
if (n < 0)
[Link]("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
f = f*c;
[Link]("Factorial of "+n+" is = "+f);
}
}
}
Q3: Write a Java program to calculate Fibonacci Series up to n numbers.
Ans:
import [Link];
class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner([Link]);
[Link]("Enter value of n:");
n = [Link]();
[Link]("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
[Link](a+" ");
}
}
}
4
Q4: Write a Java program to find out whether the given String is Palindrome or not.
Ans:
import [Link].*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("Enter a string to check if it's a palindrome");
original = [Link]();
int length = [Link]();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + [Link](i);
if ([Link](reverse))
[Link]("The string is a palindrome.");
else
[Link]("The string isn't a palindrome.");
}
}
Q5: Write a Java Program to reverse the letters present in the given String.
Ans:
import [Link].*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("Enter a string to reverse");
original = [Link]();
int length = [Link]();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + [Link](i);
[Link]("Reverse of the string: " + reverse);
}
}
5
Q6: Write a program in Java for Diamond Pattern.
Ans:
import [Link];
class Diamond
{
public static void main(String args[])
{
int n, i, j, space = 1;
[Link]("Enter the number of rows: ");
Scanner s = new Scanner([Link]);
n = [Link]();
space = n - 1;
for (j = 1; j <= n; j++)
{
for (i = 1; i <= space; i++)
{
[Link](" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
[Link]("*");
}
[Link]("");
}
space = 1;
for (j = 1; j <= n - 1; j++)
{
for (i = 1; i <= space; i++)
{
[Link](" ");
}
space++;
for (i = 1; i <= 2 * (n - j) - 1; i++)
{
[Link]("*");
}
[Link]("");
}
}
}
6
Q7. Write a Java Program to check whether the given array is Mirror Inverse or not.
Ans:
class MirrorInverse
{
static boolean isMirrorInverse(int arr[])
{
for (int i = 0; i<[Link]; i++)
{
if (arr[arr[i]] != i)
return false;
}
return true;
}
public static void main(String[] args)
{
int arr[] = {3,4,2,0,1};
if (isMirrorInverse(arr))
[Link]("Yes");
else
[Link]("No");
}
}
Q8: Write a Java program to calculate Permutation and Combination of 2 numbers.
Ans:
import [Link];
class percomb
{
public static int fact(int num)
{
int fact=1, i;
for(i=1; i<=num; i++)
{
fact = fact*i;
}
return fact;
}
public static void main(String args[])
{
int n, r;
Scanner scan = new Scanner([Link]);
[Link]("Enter Value of n : ");
n = [Link]();
7
[Link]("Enter Value of r : ");
r = [Link]();
[Link]("NCR = " +(fact(n)/(fact(n-r)*fact(r))));
[Link]("nNPR = " +(fact(n)/(fact(n-r))));
}
}
8
Lab Exercise – 3
Q1: Test Inheritance
Ans:
class Mother
{
int x;
void show()
{
[Link]("I am Mother");
}
}
class Child extends Mother
{
}
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
[Link]();
Child ch = new Child();
[Link]();
}
}
Q2: Test Overriding
Ans:
class Mother
{
int x;
void show()
{
[Link]("I am Mother");
}
}
class Child extends Mother
{
void show()
{
[Link]("I am Child");
}
}
9
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
[Link]();
Child ch = new Child();
[Link]();
}
Q3: Test Polymorphism
Ans:
class Child extends Mother
{
void show()
{
[Link]("I am Child");
}
}
class Mother
{
int x;
void show()
{
[Link]("I am Mother");
}
}
public class Main
{
public static void main (String args[]){
Mother m1 = new Child();
[Link]();
}
}
Q4: Parameterized Constructor
Ans:
class One
{
int x;
public One(int a)
{
this.x = a;
}
void show()
10
{
[Link](x);
}
}
class Two extends One
{
public Two()
{
super(10);
}
}
public class Main
{
public static void main (String args[]){
One o = new One(3);
[Link]();
Two t = new Two();
[Link]();
}
}
11