0% found this document useful (0 votes)
14 views

Java Ca

Uploaded by

VK LAKSHMANDEV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Ca

Uploaded by

VK LAKSHMANDEV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

VK.

LAKSHMANDEV 24I433

DATE : 23/10/2024

23I311 - JAVA PROGRAMMING LABORATORY

CA 1

SET: 1:

MISSING POSITIVE NUMBER

PROGRAM:

import java.util.*;

class s {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

int[] nums = new int[n];

for (int i = 0; i < n; i++) {

nums[i] = scanner.nextInt();

boolean[] found = new boolean[n + 1];

for (int num : nums) {

if (num > 0 && num <= n) {

found[num] = true;

int missingPositive = 1;

for (int i = 1; i <= n; i++) {

if (!found[i]) {

missingPositive = i;

break;

}}

if (missingPositive == n) {

missingPositive = n + 1;

System.out.println(missingPositive);}}
VK.LAKSHMANDEV 24I433

OUTPUT:

SET : 2

ATM:

PROGRAM:

import java.util.*;

public class ATM {

public static final int[] denominations = {20, 50, 100, 200, 500};

public static int[] banknotes = new int[5];

static {

Scanner scanner = new Scanner(System.in);

for (int i = 0; i < 5; i++) {

banknotes[i] = 0;

for (int i = 0; i < 5; i++) {

banknotes[i] += scanner.nextInt();

int amount = scanner.nextInt();

int[] result = new int[5];

int[] tempBanknotes = banknotes.clone();

for (int i = 4; i >= 0; i--) {

int maxNotes = amount / denominations[i];

int notesToUse = Math.min(maxNotes, tempBanknotes[i]);

result[i] = notesToUse;

amount -= notesToUse * denominations[i];


VK.LAKSHMANDEV 24I433

if (amount == 0) {

for (int i = 0; i < 5; i++) {

banknotes[i] -= result[i];

scanner.close();

public static void main(String[] args) {

OUTPUT:

SET : 3

DIAGONAL AND ADJACENT SUM

PROGRAM:

import java.util.*;

import java.util.*;

class m {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);


VK.LAKSHMANDEV 24I433

int rs = s.nextInt();

int cs = s.nextInt();

int[][] a = new int[rs][cs];

for (int i = 0; i < rs; i++) {

for (int j = 0; j < cs; j++) {

a[i][j] = s.nextInt();

for (int i = 0; i < rs; i++) {

for (int j = 0; j < cs; j++) {

System.out.print(a[i][j] + " ");

System.out.println();

int target = s.nextInt();

boolean found = false;

for (int i = 0; i < rs; i++) {

for (int j = 0; j < cs; j++) {

if (a[i][j] == target) {

found = true;

if (j - 1 >= 0 && j + 1 < cs && i + 1 < rs && i - 1 >= 0 && j - 1 >= 0 && j + 1 < cs && i + 1 < rs
&& i - 1 >= 0 ) {

System.out.println(a[i][j - 1] + a[i][j + 1] + a[i + 1][j] + a[i - 1][j]);

System.out.println(a[i - 1][j - 1] + a[i - 1][j + 1] + a[i + 1][j - 1] + a[i + 1][j + 1]);}

else System.out.println("0");

if (!found) {

System.out.println("0");

}
VK.LAKSHMANDEV 24I433

OUTPUT:

SET: 4:

STRING OPERATIONS:

import java.util.*;

class str{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

String e = s.nextLine();

char[] arr = e.toCharArray();

int count = arr.length;

System.out.println(count);

String[] w = e.split("\\s+");

int wl =w.length;

System.out.println(wl);

int count1 = 0;

for (char c:arr){

if (c == '.' || c == '!' || c == '?') {

count1+=1;

}
VK.LAKSHMANDEV 24I433

System.out.println(count1);

OUTPUT:

SET : 5

VIRUS INFECTION:

PROGRAM:

import java.util.*;

class v {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String strengths = scanner.nextLine();

int iterations = 0;

int n = strengths.length();

String current = strengths;

while (true) {

char[] next = current.toCharArray();

boolean changed = false;

for (int i = 0; i < n; i++) {

int strongest = current.charAt(i) - '0';

for (int j = i - 1; j >= 0; j--) {

if (strongest - (current.charAt(j) - '0') >= i - j) {

strongest = Math.max(strongest, current.charAt(j) - '0');

} else {
VK.LAKSHMANDEV 24I433

break;

for (int j = i + 1; j < n; j++) {

if (strongest - (current.charAt(j) - '0') >= j - i) {

strongest = Math.max(strongest, current.charAt(j) - '0');

} else {

break;

next[i] = (char) (strongest + '0');

if (next[i] != current.charAt(i)) {

changed = true;

current = new String(next);

iterations++;

if (!changed) {

break;

System.out.println(iterations);

OUTPUT:
VK.LAKSHMANDEV 24I433

SET :6:

QUADRUPLETS

PROGRAM

import java.util.*;

class a1{

public static void main(String[] args){

Scanner s =new Scanner(System.in);

int size =s.nextInt();

int[] a = new int[size];

for (int i=0;i<size;i++){

a[i] = s.nextInt();

int ta = s.nextInt();

List<int[]> result = new ArrayList<>();

for (int i=0;i<size-3;i++){

for (int j=i+1;j<size-2;j++){

for (int k= j+1;k<size-1;k++){

for (int z = k+1;z<size;z++){

if (a[i]+a[j]+a[k]+a[z] == ta){

result.add(new int[]{a[i],a[j],a[k],a[z]});

for (int[] com:result){

System.out.println(Arrays.toString(com));

}
VK.LAKSHMANDEV 24I433

OUTPUT:

SET: 7

BALANCED PARANTHESIS:

PROGRAM:

import java.util.*;

class a{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

String size = s.nextLine();

int rc=0,cc=0,sc=0;

for(int j=0;j<size.length();j++){

char i = size.charAt(j);

if (i == '{'){

cc +=1;

else if(i == '}'){

cc -= 1;

if (cc < 0){

System.out.println("false");

else if (i=='['){

sc +=1;

}
VK.LAKSHMANDEV 24I433

else if (i==']'){

sc-=1;

if (sc <0){

System.out.println("false");

else if (i=='('){

rc +=1;

else if (i==')'){

rc -=1;

if (rc<0){

System.out.println("false");

if (rc == 0 && cc == 0 && sc ==0){

System.out.println("true");

else{

System.out.println("false");

}}

OUTPUT:
VK.LAKSHMANDEV 24I433

SET : 8:

HTML TAGS:

PROGRAM:

import java.util.*;

public class op {

private static String input;

static {

Scanner scanner = new Scanner(System.in);

input = scanner.nextLine();

StringBuilder result = new StringBuilder();

StringBuilder tagName = new StringBuilder();

StringBuilder tempContent = new StringBuilder();

boolean isStartTag = false;

boolean isEndTag = false;

boolean invalidContent = false;

String openTag = "";

for (int i = 0; i < input.length(); i++) {

char ch = input.charAt(i);

if (ch == '<') {

if (tempContent.length() > 0 && openTag.isEmpty()) {

result.append(tempContent);

tempContent.setLength(0);

tagName.setLength(0);

isStartTag = true;

isEndTag = (i + 1 < input.length() && input.charAt(i + 1) == '/');

if (isEndTag) i++;

} else if (ch == '>') {

isStartTag = false;

String currentTag = tagName.toString();


VK.LAKSHMANDEV 24I433

if (isEndTag) {

if (openTag.equals(currentTag)) {

openTag = "";

result.append(tempContent);

tempContent.setLength(0);

} else {

invalidContent = true;

break;

} else {

if (openTag.isEmpty()) {

openTag = currentTag;

} else {

tempContent.setLength(0);

} else if (isStartTag) {

tagName.append(ch);

} else if (openTag.isEmpty()) {

result.append(ch);

} else {

tempContent.append(ch);

if (invalidContent || !openTag.isEmpty()) {

System.out.println("Output: None");

} else {

System.out.println("Output: " + (result.length() > 0 ? result.toString().trim() : "None"));

}
VK.LAKSHMANDEV 24I433

public static void main(String[] args) {

OUTPUT:

You might also like