In: Computer Science
Input Format
4 1 2 3 4
Constraints
there will be no more than 50 input numbers.
Output Format
Odd: 2 Even: 2 Divisible by 7: 0 Average: 250
Sample Input 0
4 1 2 3 4
Sample Output 0
Odd: 2 Even: 2 Divisible by 7: 0 Average: 250
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
String[] arrTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
int arrItem = Integer.parseInt(arrTemp[i]);
arr.add(arrItem);
}
// You now have an array of integers called arr. You should process the array to achieve the results.
// note that you can refer to items in arr using standard Java array notation
// arr[0] will give you the item at location 0 in this array
// Write your code here
bufferedReader.close();
}
}
CODE :
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
String[] arrTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
int arrItem = Integer.parseInt(arrTemp[i]);
arr.add(arrItem);
}
// You now have an array of integers called arr. You should process the array to achieve the results.
// note that you can refer to items in arr using standard Java array notation
// arr[0] will give you the item at location 0 in this array
int e = 0, o = 0, s = 0, d = 0;
for(int i = 0; i < arr.size(); i++)
{
if(arr.get(i) % 2 == 0)
{
e++;
}
if(arr.get(i) % 2 != 0)
{
o++;
}
if(arr.get(i) % 7 == 0)
{
d++;
}
s += arr.get(i);
}
System.out.println(s);
System.out.println("Odd : " + o);
System.out.println("Even : " + e);
System.out.println("Divisible by 7 : " + d);
System.out.println("Average : " + (float)s/n);
bufferedReader.close();
}
}
OUTPUT :