AP Lab2
AP Lab2
AP Lab2
LAB RECORD
Submitted by
Jaivardhan Singh(201B128)
Batch B4(BX)
2020-2021
1
S. No. Date Lab Exercise Page No. Rem
INDEX
2
LAB Exercise -1
System.out.println();
}
}
}
3
System.out.print(" ");
}
System.out.println();
}
}
}
3. Write a program in Java to print the table of a number received through command line
argument, e.g.
If user gives 4 through command line argument, the program should print:
4 x 1= 4
4 x 2= 8
............
4x10= 40
public class Main
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
for(int i=1;i<=10;i++)
{
System.out.println("4 x "+i+"="+i*a);
}
}
}
LAB Exercise 2
4
1. Write a Java program to perform basic calculator operations.
import java.util.Scanner;
public class Calculator
{
public static void main (String[]args)
{
Scanner reader = new Scanner (System.in);
System.out.print ("Enter two numbers: ");
double first = reader.nextDouble ();
double second = reader.nextDouble ();
System.out.print ("Enter an operator (+, -, *, /): ");
char operator = reader.next ().charAt (0);
double result;
switch (operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
default:
System.out.printf ("Error! operator is not correct");
return;
}
System.out.printf ("%.1f %c %.1f = %.1f", first, operator, second, result);
}}
5
int output;
if (n == 1)
{
return 1;
}
output = fact (n - 1) * n;
return output;
}
}
3. Write a Java program to calculate Fibonacci Series up to n numbers.
public class Fibonacci
{
public static void main (String[]args)
{
int n = 100, t1 = 0, t2 = 1;
System.out.print ("Upto " + n + ": ");
while (t1 <= n)
{
System.out.print (t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
4. Write a Java program to find out whether the given String is Palindrome or not.
import java.util.Scanner;
public class Palindrome
{
static void checkPalindrome (String input)
{
boolean res = true;
int length = input.length ();
for (int i = 0; i <= length / 2; i++)
{
if (input.charAt (i) != input.charAt (length - i - 1))
{
res = false;
break;
}
}
System.out.println (input + " is palindrome = " + res);
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
System.out.print ("Enter your Statement: ");
String str = sc.nextLine ();
6
checkPalindrome (str);
}
}
5. Write a Java program to calculate Permutation and Combination of 2 numbers.
import java.util.Scanner;
public class nprandncr
{
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 (System.in);
System.out.print ("Enter Value of n : ");
n = scan.nextInt ();
System.out.print ("Enter Value of r : ");
r = scan.nextInt ();
System.out.print ("NCR = " + (fact (n) / (fact (n - r) * fact (r))));
System.out.print ("nNPR = " + (fact (n) / (fact (n - r))));
}
}
7
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
PatternA a = new PatternA ();
a.display (7);
}
}
Diamond
import java.util.Scanner;
public class DiamondPattern
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j<= n; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j<= n - 1; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i<= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
8
7. Write a Java program to reverse the letters present in the given string.
public class stringreverse
{
public static void main(String[] args)
{
String str = "Welcome To Edureka";
String[] strArray = str.split(" ");
for (String temp: strArray)
{
System.out.println(temp);
}
for(int i=0; i<3; i++){ char[] s1 = strArray[i].toCharArray(); for (int j = s1.length-1; j>=0; j--)
{
System.out.print(s1[j]);
}
System.out.print(" ");
}
}
}
8. Write a Java program to check whether the given array is Mirror Inverse or not.
public class MirrorInverse {
static boolean isMirrorInverse(int arr[])
{
for (int i = 0; i<arr.length; i++) {
if (arr[arr[i]] != i)
return false;
}
return true;
}
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 0 };
if (isMirrorInverse(arr))
System.out.println("Yes");
else
System.out.println("No");
}
}
9
LAB Exercise 3
1. Test for inheritance
class Motherther{
int x=4;
void show()
{
System.out.println("The value of x = "+x);
}
}
10
3. Test for Polymorphism
class Motherther{
void show()
{
System.out.println("Hello World!"");
}
}
(b)
class Motherther{
void show()
{
System.out.println("Hello World!"");
}
}
11
ch.show();
Mother m1 = new Child();
m1.show();
}
}
4. Parameterized Constructor
class One{
One(int x)
{
System.out.println("Hello World" + x);
}
}
12