solved java
class Main{
public static void main(String[] args){
int [] arr={1,2,3,4};
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
if((arr[i]+arr[j])%2==0){
System.out.println("Even: "+arr[i]+"+"+arr[j]);
}
}
}
}
}
//finding maximum and minimum:
import java.util.*;
class SecondMax {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5}; // You can test with other arrays too
int max = Integer.MIN_VALUE;
int second_max = Integer.MIN_VALUE;
for (int num : arr) {
if (num > max) {
second_max = max;
max = num;
} else if (num > second_max && num != max) {
second_max = num;
}
}
System.out.println("Max: " + max);
System.out.println("Second Max: " + second_max);
}
}
//max and second max by using normal for loop:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={1,2,3,4,5};
//finging max and second max value
int max=Integer.MIN_VALUE;
int second_max=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>max){
second_max=max;
max=arr[i];
}
else if(arr[i]>second_max && arr[i]<max){
second_max=arr[i];
}
}
System.out.println(max);
System.out.println(second_max);
}
}
//for second max and second min:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={1,2,3,4,5};
//finging max and second max value
int max=Integer.MIN_VALUE;
int second_max=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE;
int second_min =Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>max){
second_max=max;
max=arr[i];
}
else if(arr[i]>second_max && arr[i]<max){
second_max=arr[i];
}
}
for(int num:arr){
if (num<min){
second_min=min;
min=num;
}
else if(num<second_min && num!=min){
second_min=num;
}
}
System.out.println(max);
System.out.println(second_max);
System.out.println(min);
System.out.println(second_min);
}
}
Comments
Post a Comment