1
Practical No. 1:
Write a java program to take input as a command line argument. Your name, course,
universityrollno and semester. Display the information.
Source Code:
public class studentDetails {
public static void main(String args[]) {
if (args.length != 4) {
System.out.println("Enter all the fields!");
String Name = args[0];
String UniversityRollNo = args[1];
String Course = args[2];
String Semester = args[3];
System.out.println("Name: " + Name);
System.out.println("UniversityRollNo: " + UniversityRollNo);
System.out.println("Course: " + Course);
System.out.println("Semester: " + Semester);
Aditya Kalura Section:B2 Roll no.: 6
2
Output:
Aditya Kalura Section:B2 Roll no.: 6
3
Practical No. 2:
Using the switch statement, write a menu-driven program to calculate the maturity amount of
a bank deposit.
The user is (i) Term Deposit (ii) Recurring Deposit For option (i) accept Principal (p), rate of
interest (r) and time period in years (n). Calculate and output the maturity amount (a)
receivable using the formula a = p[1 + r / 100]n.
For option (ii) accept monthly installment (p), rate of interest (r) and time period in months
(n). Calculate and output the maturity amount (a) receivable using the formula a = p * n + p *
n(n + 1) / 2 * r / 100 * 1 / 12. For an incorrect option, an appropriate error message should be
displayed.
Source Code:
import java.util.*;
public class maturityAmount {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("(1) Term Deposit");
System.out.println("(2) Recurring Deposit");
System.out.println("(3) Exit");
int choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.print("Principle rate :=> ");
int p = sc.nextInt();
System.out.print("rate of intrest :=> ");
int r = sc.nextInt();
System.out.print("Time period in year :=> ");
int n = sc.nextInt();
int ammount = (int) (p * Math.pow(1 + r / 100.0, n));
System.out.println("maturity amount" + ammount);
Aditya Kalura Section:B2 Roll no.: 6
4
break;
case 2: {
System.out.print("Principle rate :=> ");
int p = sc.nextInt();
System.out.print("rate of intrest :=> ");
int r = sc.nextInt();
System.out.print("Time period in months :=> ");
int n = sc.nextInt();
int ammount = (p * n) + (p * n * ((n + 1) / 2) * (r / 100) * (1 / 12));
System.out.println("maturity amount" + ammount);
break;
}
case 3: {
exit = true;
break;
default: {
System.out.println("Error :=> Incorrect choice Entered\nexiting...");
exit = true;
break;
sc.close();
}
}
Aditya Kalura Section:B2 Roll no.: 6
5
Output:
Practical:3
Aditya Kalura Section:B2 Roll no.: 6
6
Program to find if the given numbers are Friendly pair or not (Amicable or not). Friendly Pair
are two or more numbers with a common abundance.
Source Code:
import java.util.Scanner;
class friendlyPair {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
int num1 = sc.nextInt();
System.out.println("Enter second number: ");
int num2 = sc.nextInt();
int sum1 = 0;
int sum2 = 0;
for (int i = 1; i < num1; i++) {
if (num1 % i == 0) {
sum1 += i;
for (int i = 1; i < num2; i++) {
if (num2 % i == 0) {
sum2 += i;
}
double ratio1 = (double) sum1 / num1;
double ratio2 = (double) sum2 / num2;
if (ratio1 == ratio2) {
System.out.println("Friendly pair");
} else {
System.out.println("Not a friendly pair");
Aditya Kalura Section:B2 Roll no.: 6
7
sc.close();
Aditya Kalura Section:B2 Roll no.: 6
8
Output:
Aditya Kalura Section:B2 Roll no.: 6
9
Q4. Program to replace all 0's with 1 in a given integer. Given an integer as an input, all the
0's in the number has to be replaced with 1
SOURCE CODE:
import java.util.Scanner;
public class Replace {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int result = 0;
int placeValue = 1;
while (num > 0) {
int digit = num % 10;
if (digit == 0) {
digit = 1;
result = result + (digit * placeValue);
placeValue *= 10;
num /= 10;
System.out.println("Modified number: " + result);
scanner.close();
Aditya Kalura Section:B2 Roll no.: 6
10
Output:
Practical 5:
Aditya Kalura Section:B2 Roll no.: 6
11
Printing an array into Zigzag fashion. Suppose you were given an array of integers, and you
are told to sort the integers in a zigzag pattern. In general, in a zigzag pattern, the first integer
is less than the second integer, which is greater than the third integer, which is less than the
fourth integer, and so on. Hence, the converted array should be in the form of e1 < e2 > e3 <
e4 > e5 < e6.
SOURCE CODE:
import java.util.Scanner;
public class zigzag {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
for (int i = 1; i < arr.length - 1; i = i + 2) {
if (!(arr[i] > arr[i - 1])) {
int temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
if (!(arr[i] > arr[i + 1])) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
Aditya Kalura Section:B2 Roll no.: 6
12
System.out.println("Modified array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
scanner.close();
Aditya Kalura Section:B2 Roll no.: 6
13
Output:
Aditya Kalura Section:B2 Roll no.: 6
14
Practical 6:
The problem to rearrange positive and negative numbers in an array . Method: This approach
moves all negative numbers to the beginning and positive numbers to the end but changes the
order of appearance of the elements of the array.
SOURCE CODE:
import java.util.Scanner;
public class rearrange {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int num = sc.nextInt();
int[] arr = new int[num];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < num; i++) {
arr[i] = sc.nextInt();
}
System.out.print("The original array is: "); // printing the original array
for (int i = 0; i < num; i++) {
System.out.print(arr[i] + " ");
int i = 0;
int j = 0;
while (i < num) {
while (i < num && arr[i] < 0) {
i++;
j = i;
while (j < num && arr[j] >= 0) {
j++;
}
Aditya Kalura Section:B2 Roll no.: 6
15
if (j < num) {
int temp = arr[i]; // swapping the numbers
arr[i] = arr[j];
arr[j] = temp;
}
i++;
System.out.println("\nThe rearranged array is: "); // printing rearranged array
for (int k = 0; k < num; k++) {
System.out.print(arr[k] + " ");
}
sc.close();
}
Aditya Kalura Section:B2 Roll no.: 6
16
Output:
Aditya Kalura Section:B2 Roll no.: 6
17
Q7. Program to find the saddle point coordinates in a given matrix. A saddle point is an
element of the matrix, which is the minimum element in its row and the maximum in its
column.
SOURCE CODE:
import java.util.Scanner;
public class Saddle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows and columns: ");
int rows = sc.nextInt();
int columns = sc.nextInt();
int arr[][] = new int[rows][columns];
System.out.println("Enter array elements: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
arr[i][j] = sc.nextInt();
sc.close();
boolean found = false;
for (int i = 0; i < rows; i++) {
int minRow = arr[i][0];
int colIdx = 0;
for (int j = 1; j < columns; j++) {
if (arr[i][j] < minRow) {
minRow = arr[i][j];
colIdx = j;
}
Aditya Kalura Section:B2 Roll no.: 6
18
boolean isSaddle = true;
for (int k = 0; k < rows; k++) {
if (arr[k][colIdx] > minRow) {
isSaddle = false;
break;
if (isSaddle) {
System.out.println("Saddle point found: " + minRow + " at (" + i + ", " + colIdx +
")");
found = true;
}
if (!found) {
System.out.println("Saddle point not found.");
Aditya Kalura Section:B2 Roll no.: 6
19
Output:
Aditya Kalura Section:B2 Roll no.: 6