Vrinew
Vrinew
SEC: I2
ROLL.NO: 127
Q6. 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.
Steps:
1. Declare an array and input the array elements.
2. Start traversing the array and if the current element is negative, swap the current element
with the first positive element and continue traversing until all the elements have been
encountered.
3. Print the rearranged array.
Test case:
Input: 1-1 2-2 3-3
Output: -1-2-313 2
SOL:
import java.util.*;
public class ques_6 {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter array size: ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("enter elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int j=0;
for(int i=0; i<n; i++) {
if(arr[i]<0){
if(i!=j){
swap(arr, i, j);
}
j++;
}
}
for(int i=0; i<n; i++){
System.out.print(arr[i]+" ");
}
}
public static void swap(int ar[], int a, int b){
int temp = ar[a];
ar[a] = ar[b];
ar[b] = temp;
}
}
OUTPUT:
NAME: VRINDAA SHARMA
SEC: I2
ROLL.NO: 127Q7. 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.
For example, consider the matrix given below
Mat[3][3]
123
456
789 SOL:
import java.util.Scanner;
public class ques_7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[3][3];
System.out.println("Enter the matrix elements:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = scanner.nextInt();
}
}
boolean found = false;
for (int i = 0; i < 3; i++) {
int minRowIndex = 0;
for (int j = 1; j < 3; j++) {
if (matrix[i][j] < matrix[i][minRowIndex]) {
minRowIndex = j;
}
}
boolean isSaddlePoint = true;
for (int k = 0; k < 3; k++) {
if (matrix[k][minRowIndex] > matrix[i][minRowIndex]) {
isSaddlePoint = false;
break;
}
}
if (isSaddlePoint) {
System.out.println("Saddle Point found at (" + i + ", " + minRowIndex + ") with
value " + matrix[i][minRowIndex]);
found = true;
break;
}
}
if (!found) {
System.out.println("No Saddle Point found.");
}
scanner.close();
}
}
OUTPUT:
NAME: VRINDAA SHARMA
SEC: I2
ROLL.NO: 127
Q8. Program to find all the patterns of 0(1+)0 in the given string. Given a string containing
0's and 1's, find the total number of 0(1+)0 patterns in the string and output it. 0(1+)0 - There
should be at least one '1' between the two 0's.
For example, consider the following string.
Input: 01101111010
Output: 3
Explanation:
01101111010 - count = 1
01101111010 - count = 2
01101111010-count = 3
SOL:
import java.util.*;
public class ques_8{
public static int pat(String ab) {
int count = 0;
for (int i = 0; i < ab.length() - 1; i++) {
if (ab.charAt(i) == '0') {
int j = i + 1;
while (j < ab.length() && ab.charAt(j) == '1') {
j++;
}
if (j > i + 1 && j < ab.length() && ab.charAt(j) == '0') {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the string: ");
String in = scanner.nextLine();
scanner.close();
int res = pat(in);
System.out.println("Input: " + in);
System.out.println("Output: " + res);
}
}
OUTPUT:
NAME: VRINDAA SHARMA
SEC: I2
ROLL.NO: 127
Q9. Write a java program to create a class named 'Bank ' with the following data members:
Name of depositor Address of depositor Account Number Balance in account Class 'Bank'
has a method for each of the following:
1 - Generate a unique account number for each depositor For first depositor, account number
will be 1001, for second depositor it will be 1002 and so on
2 - Display information and balance of depositor
3 - Deposit more amount in balance of any depositor
4 - Withdraw some amount from balance deposited
5 - Change address of depositor After creating the class, do the following operations
1 - Enter the information (name, address, account number, balance) of the depositors.
Number of depositors is to be entered by user.
2 - Print the information of any depositor.
3 - Add some amount to the account of any depositor and then display final information of
that depositor
4 - Remove some amount from the account of any depositor and then display final
information of that depositor
5 - Change the address of any depositor and then display the final information of that
depositor
6 - Randomly repeat these processes for some other bank accounts
SOL:
import java.util.Scanner;
class Bank {
private static int accountCounter = 1000;
private String name;
private String address;
private int accountNumber;
private double balance;
public Bank(String name, String address, double balance) {
this.name = name;
this.address = address;
this.accountNumber = ++accountCounter;
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public void displayInfo() {
System.out.println("\nAccount Number: " + accountNumber);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Balance: " + balance);
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Amount deposited successfully.");
} else {
System.out.println("Invalid deposit amount.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Amount withdrawn successfully.");
} else {
System.out.println("Invalid withdrawal amount or insufficient balance.");
}
}
public void changeAddress(String newAddress) {
this.address = newAddress;
System.out.println("Address updated successfully.");
}
}
public class ques_9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of depositors: ");
int numDepositors = scanner.nextInt();
scanner.nextLine();
Bank[] accounts = new Bank[numDepositors];
for (int i = 0; i < numDepositors; i++) {
System.out.println("\nEnter details for depositor " + (i + 1));
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Address: ");
String address = scanner.nextLine();
System.out.print("Initial Balance: ");
double balance = scanner.nextDouble();
scanner.nextLine();
accounts[i] = new Bank(name, address, balance);
System.out.println("Account created successfully! Account Number: " +
accounts[i].getAccountNumber());
}
while (true) {
System.out.println("\nSelect an option:");
System.out.println("1. Display depositor info");
System.out.println("2. Deposit amount");
System.out.println("3. Withdraw amount");
System.out.println("4. Change address");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
if (choice == 5) {
System.out.println("Exiting program.");
break;
}
System.out.print("Enter account number: ");
int accNum = scanner.nextInt();
scanner.nextLine();
Bank selectedAccount = null;
for (int i = 0; i < numDepositors; i++) {
if (accounts[i].getAccountNumber() == accNum) {
selectedAccount = accounts[i];
break;
}
}
if (selectedAccount == null) {
System.out.println("Account not found!");
continue;
}
switch (choice) {
case 1:
selectedAccount.displayInfo();
break;
case 2:
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
selectedAccount.deposit(depositAmount);
selectedAccount.displayInfo();
break;
case 3:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
selectedAccount.withdraw(withdrawAmount);
selectedAccount.displayInfo();
break;
case 4:
System.out.print("Enter new address: ");
String newAddress = scanner.nextLine();
selectedAccount.changeAddress(newAddress);
selectedAccount.displayInfo();
break;
default:
System.out.println("Invalid choice! Try again.");
}
}
scanner.close();
}
}
OUTPUT: