21BCS10193_Arjun Kumar_Day_7

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

DEPARTMENT OF

COMPUTERSCIENCE& ENGINEERING
Day 7: Domain Camp

Student Name: Lakshay Sindhu UID: 21BCS10420


Branch: CSE Section/Group: 628 B
Semester: 6 Date of Performance: 2/7/24
Subject Name: Java

Q1. Add Digits

Code:
public class Solution {
public int addDigits(int num)
{ if (num == 0) {
return 0;
}
return 1 + (num - 1) % 9;
}

public static void main(String[] args)


{ Solution solution = new Solution();

System.out.println(solution.addDigits(38));
System.out.println(solution.addDigits(0));
}
}

Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Q2. Find the Difference

Code:
public class Solution {
public char findTheDifference(String s, String t)
{ int xor = 0;

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


{ xor ^= s.charAt(i);
}

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


{ xor ^= t.charAt(i);
}

return (char) xor;


}

public static void main(String[] args)


{ Solution solution = new Solution();

System.out.println(solution.findTheDifference("abcd", "abcde"));
System.out.println(solution.findTheDifference("", "y"));
}
}

Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 3: Can You Access?

Code:
public class Solution {
public char findTheDifference(String s, String t)
{ int xor = 0;

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


{ xor ^= s.charAt(i);
}

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


{ xor ^= t.charAt(i);
}

return (char) xor;


}

public static void main(String[] args)


{ Solution solution = new Solution();

// Test cases
System.out.println(solution.findTheDifference("abcd", "abcde"));
System.out.println(solution.findTheDifference("", "y"));
}
}

Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 4: ATM Problem

Code:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int X = scanner.nextInt();
double Y = scanner.nextDouble();

double charge = 0.50;

if (X % 5 == 0 && Y >= X + charge)


{ Y = Y - X - charge;
}
System.out.printf("%.2f", Y);

scanner.close();
}
}
Output

Ques 5: Java String Tokens

Code:
import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
scan.close();
String[] tokens = s.split("[^A-Za-z]+"); List<String>

tokenList = new ArrayList<>(); for (String token :


tokens) {
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
{ tokenList.add(token);
}
}
System.out.println(tokenList.size())
; for (String token : tokenList) {
System.out.println(token);
}
}
}

Output:

Ques 6. Roman to Integer

Code:
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int romanToInt(String s) {
Map<Character, Integer> romanMap = new HashMap<>();
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING

int prevValue = 0;
for (int i = 0; i < s.length(); i++) {
int currentValue = romanMap.get(s.charAt(i));
if (prevValue < currentValue) {
total += currentValue - 2 * prevValue;
} else {
total += currentValue;
}
prevValue = currentValue;
}

return total;
}

public static void main(String[] args)


{ Solution solution = new Solution();
System.out.println(solution.romanToInt("III"));
System.out.println(solution.romanToInt("LVIII"));
System.out.println(solution.romanToInt("MCMXCIV"));
}
}

Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 7: Pow(x, n)

Code:
public class Solution {
public double myPow(double x, int n) {
if (n == Integer.MIN_VALUE) {
x = x * x;
n = n / 2;
}
if (n < 0) {
x = 1 /
x; n = -
n;
}
double result = 1;
double currentProduct = x;

while (n > 0) {
if (n % 2 == 1) {
result *= currentProduct;
}
currentProduct *= currentProduct;
n /= 2;
}
return result;
}
public static void main(String[] args)
{ Solution solution = new Solution();
System.out.println(solution.myPow(2.00000, 10));
System.out.println(solution.myPow(2.10000, 3));
System.out.println(solution.myPow(2.00000, -2));
}
}

Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 8. Missing Number

Code:
public class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;

int expectedSum = n * (n + 1) / 2;
int actualSum = 0;

for (int num : nums)


{ actualSum +=
num;
}

return expectedSum - actualSum;


}

public static void main(String[] args)


{ Solution solution = new Solution();

int[] nums1 = {3, 0, 1};


System.out.println(solution.missingNumber(nums1));

int[] nums2 = {0, 1};


System.out.println(solution.missingNumber(nums2));

int[] nums3 = {9, 6, 4, 2, 3, 5, 7, 0, 1};


System.out.println(solution.missingNumber(nums3));
}
}
Output:

You might also like