Advertisement
saurav_kalsoor

Count Odd Digits - JAVA

Jul 27th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Problem {
  7.  
  8.     public static void main(String[] args){
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         ArrayList<Integer> list = new ArrayList<>();
  12.  
  13.         for(int i=0; i<n; i++){
  14.             int num = sc.nextInt();
  15.             list.add(num);
  16.         }
  17.  
  18.         System.out.println(countOddDigitNumbers(list, n));
  19.     }
  20.  
  21.     public static int countOddDigitNumbers(ArrayList<Integer> list, int n){
  22.         int count = 0;
  23.         for(int num : list){
  24.             if(hasOddDigits(num))
  25.                 count++;
  26.         }
  27.         return count;
  28.     }
  29.  
  30.     public static boolean hasOddDigits(int num){
  31.         int digits = 0;
  32.         while (num > 0){
  33.             digits++;
  34.             num /= 10;
  35.         }
  36.         return digits%2 == 1;
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement