Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

DAY5 Java

yes

Uploaded by

donreturnwell
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

DAY5 Java

yes

Uploaded by

donreturnwell
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENTOF

COMPUTERSCIENCE&ENGINEERING

DAY- 5
Student Name: Himanshu UID: 21BCS5642
Branch: CSE Section/Group: 904-B
Subject Name: JAVA Date: 03/06/24

Ques 1) Java Strings Introduction


import java.io.*;
import java.util.*;

public class Solution {


public static void main(String[] args) {

Scanner sc=new Scanner(System.in);


String A=sc.next();
String B=sc.next();
/* Enter your code here. Print output to STDOUT. */
System.out.println(A.length() + B.length());
if(A.compareTo(B)>0){
System.out.println("Yes");
}
else{
System.out.println("No");
}
System.out.print(Character.toUpperCase(A.charAt(0)) +A.substring(1)+"
"+Character.toUpperCase(B.charAt(0)) +B.substring(1));
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

Ques 2) Add Strings


class Solution {
public String addStrings(String num1, String num2) {
StringBuilder str = new StringBuilder();

int i = num1.length() - 1, j = num2.length() - 1, carry = 0, sum;

while (i >= 0 || j >= 0 || carry > 0) {


int digit1 = (i >= 0) ? num1.charAt(i--) - '0' : 0;
int digit2 = (j >= 0) ? num2.charAt(j--) - '0' : 0;

sum = digit1 + digit2 + carry;


str.append(sum % 10);
carry = sum / 10;
}

return str.reverse().toString();
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

Ques 3) Largest Number


class Solution {
public String largestNumber(int[] nums) {
String[] s = new String[nums.length];
for(int i=0; i<nums.length; i++) s[i] = String.valueOf(nums[i]);
Arrays.sort(s, (a,b) -> (b + a).compareTo(a + b));
return s[0].equals("0") ? "0" : String.join("",s);
}
}

Ques 4) Rotate Image


class Solution {
public void rotate(int[][] matrix) {
int l = matrix.length;
int i=0;
int j= l-1;
while(i<j) {
for(int count=0; i+count<j; count++){
rotate(matrix, i, j, count);
}
i++; j--;
}
}
public void rotate(int[][] matrix, int i, int j, int count ) {
int temp = matrix[i][i+count];
matrix[i][i+count] = matrix[j-count][i];
matrix[j-count][i] = matrix[j][j-count];
matrix[j][j-count] = matrix[i+count][j];
matrix[i+count][j] = temp;
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

Ques 5) Let's Review


import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {


String evenChars="";
String oddChars ="";

Scanner input = new Scanner(System.in);


int testNumber = input.nextInt();
input.nextLine(); //cosumption line for nextInt

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


String s = input.nextLine();
for(int j = 0; j<s.length();j++){
String currentChar = Character.toString(s.charAt(j));
if(j%2==0)
evenChars+=currentChar;
else
oddChars+=currentChar;
}
System.out.println(evenChars +" "+ oddChars);
evenChars ="";
oddChars ="";
}
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

You might also like