In Java Write a program that takes 4 numbers (N1, N2, N3 and N4) from the user a
ID: 673692 • Letter: I
Question
In Java
Write a program that takes 4 numbers (N1, N2, N3 and N4) from the user and prints a message based on the relationship between the first pair and the second pair of these numbers: -'smaller' if each of N1 and N2 is strictly smaller than each of N3 and N4 - 'larger' if each of N1 and N2 is larger or equal to each of N3 and N4 'mixed' otherwise Note that for 'smaller' you must check that N1 is smaller than both N3 and N4 and that at the same time N2 is also smaller than both N3 and N4 Similarly for 'larger'. Do not use any sorting methods or functions from Python. Examples: for numbers 4, 1,8, 13 (corresponding to N1, N2, N3, N4) it should print 'smaller' (because 4 is smaller than both 8 and 13; and also 1 is smaller than both 8 and 13) for numbers 7, 9, 0, 2 it should print 'bigger' (because 7 is bigger than both 0 and 2; and also 9 is bigger than both 0 and 2) for numbers 8, 1, 4, 13 it should print 'mixed' (because the conditions for 'smaller' and 'bigger' are not met: 8 is between 4 and 13) Write a program that prompts the user for a string, S, and then creates a new string, S_new, that has every other letter from S (starting with the SECOND letter). At the end, print the new string. Example: if the user enters "catsAndDogs" the program builds and prints: "asnDg" Example: if the user enters "C" the program builds and prints: " " Write a program that prompts the user for 4 integers, A, B, C and D, and prints all the numbers between A and B that are divisible by only C or only D, but not both. For example if the user enters 2, 9, 2, 3, the program will print: 2, 3, 4, 8, 9.Explanation / Answer
Question 4
package chegg;
import java.util.Scanner;
public class Question4 {
public static void main(String[] args) {
int[] n = new int[4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 4 numbers seperated by space: ");
for(int i=0; i<4; i++)
n[i] = sc.nextInt();
if((n[0] < n[2] && n[0] < n[3]) && (n[1] < n[2] && n[1] < n[3])) {
System.out.println(" smaller");
}
else if((n[0] > n[2] && n[0] > n[3]) && (n[1] > n[2] && n[1] > n[3])) {
System.out.println(" larger");
}
else {
System.out.println(" mixed");
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION 5
package chegg;
import java.util.Scanner;
public class Question5 {
public static void main(String[] args) {
String S;
String S_new;
StringBuilder temp = new StringBuilder();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
S = sc.nextLine();
int len = S.length();
for(int i=0; i<len; i++) {
if(i%2!=0) {
temp.append(S.charAt(i));
}
}
S_new = temp.toString();
System.out.println(" Output: "+S_new);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION 6
package chegg;
import java.util.Scanner;
public class Question6 {
public static void main(String[] args) {
int[] n = new int[4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 4 numbers seperated by space: ");
for(int i=0; i<4; i++)
n[i] = sc.nextInt();
System.out.println(" OUTPUT: ");
for(int i=n[0]; i<=n[1]; i++) {
if(i%n[2] == 0 || i%n[3] == 0){
if(!(i%n[2] == 0 && i%n[3] == 0))
System.out.print(i+" ");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.