Lab Exercise - 1: Q1. Ans
Lab Exercise - 1: Q1. Ans
1
ii)
public class Main
{
public static void main (String args[])
{
int a = Integer.parseInt(args[0]);
for (int i = a; i >= 0; i--)
{
for (int j = 0; j < a - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
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 = Integer.parseInt(args[0]);
for(int i = 1; i <= 10; i++)
System.out.println(a + " X " + i + " = " + a * i);
}
}
2
Lab Exercise – 2
case '-':
res = fir - sec;
break;
case '*':
res = fir * sec;
break;
case '/':
res = fir / sec;
break;
default:
System.out.print("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", fir, oper, sec, res);
}
}
3
Ans:
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, f = 1;
System.out.println("Enter an integer to calculate its factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
f = f*c;
4
Q4: Write a Java program to find out whether the given String is Palindrome or not.
Ans:
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
if (original.equals(reverse))
System.out.println("The string is a palindrome.");
else
System.out.println("The string isn't a palindrome.");
}
}
Q5: Write a Java Program to reverse the letters present in the given String.
Ans:
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of the string: " + reverse);
}
}
5
Q6: Write a program in Java for Diamond Pattern.
Ans:
import java.util.Scanner;
class Diamond
{
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("");
}
}
}
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<arr.length; 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))
System.out.println("Yes");
else
System.out.println("No");
}
}
8
Lab Exercise – 3
}
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}
}
11