1)Accept two positive integers M and N as input. There are two cases to consider:
(1) If M < N, then print M as output.
(2) If M >= N, subtract N from M. Call the difference M1. If M1 >= N, then subtract N from M1 and call the difference M2. Keep doing this operation until you reach a value k, such that, Mk < N. You have to print the value of Mk as output.
import java.util.Scanner;
public class CodeTranslation {
public static int get(int M, int N) {
if (M < N) {
return M;
} else {
int Mk = M;
while (Mk >= N) {
Mk -= N;
}
return Mk;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
int N = scanner.nextInt();
int output = get(M, N);
System.out.println(output);
}
}
2)Accept three positive integers as input and check if they form the sides of a right triangle.
Print YES if they form one, and NO if they do not. The input will have three lines, with one integer on each line. The output should be a single line containing one of these two strings: YES or NO
(Hypotenuse)2 = (Base)2 + (Perpendicular)2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
if ((Math.pow(x, 2) + Math.pow(y, 2) == Math.pow(z, 2)) ||
(Math.pow(y, 2) + Math.pow(z, 2) == Math.pow(x, 2)) ||
(Math.pow(z, 2) + Math.pow(x, 2) == Math.pow(y, 2))) {
System.out.print("YES");
} else {
System.out.print("NO");
}
}
}
3)
Accept a string as input. Your task is to determine if the input string is a valid password or not. For a string to be a valid password, it must satisfy all the conditions given below:
(1) It should have at least 8 and at most 32 characters
(2) It should start with an uppercase or lowercase letter
(3) It should not have any of these characters: / \ = ' "
(4) It should not have spaces
It could have any character that is not mentioned in the list of characters to be avoided (points 3 and 4). Output True if the string forms a valid password and False otherwise.
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine().trim();
boolean a = true;
if (!(password.length() >= 8 && password.length() <= 32)) {
a = false;
} else {
char firstChar = password.charAt(0);
if (!(('a' <= firstChar && firstChar <= 'z') || ('A' <= firstChar && firstChar <= 'Z'))) {
a = false;
} else {
String s = "/\\='\\"";
for (int i = 0; i < password.length(); i++) {
if (s.contains(String.valueOf(password.charAt(i)))) {
a = false;
break;
}
}
if (a) {
if (password.contains(" ")) {
a = false;
}
}
}
}
System.out.println(a);
}
}
5)Accept a string as input, convert it to lower case, sort the string in alphabetical order, and print the sorted string to the console. You can assume that the string will only contain letters.
import java.util.*;
public class code {
public static void main(String[] args) {
Scanner sc = new java.util.Scanner(System.in);
String c = sc.nextLine();
c = c.toLowerCase();
char[] charArray = c.toCharArray();
Arrays.sort(charArray);
String b = new String(charArray);
System.out.println(b);
}
}
No comments:
Post a Comment