Hack Forums
[Java] Print out all odd numbers in random set of numbers ? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Print out all odd numbers in random set of numbers ? (/showthread.php?tid=5938284)



Print out all odd numbers in random set of numbers ? - cryptobassboost - Feb 6, 2019

Hey guys,

so I'm doing some assignments in the R programming language, and I think it's kinda similair to java so hoping to get help here.

Suppose you have a random set of numbers, say 's <- c(1,5,4,2)'
How would I print out all odd numbers the integer way?

I can do it using booleans, like this:
s[lapply(s, "%%", 2) != 0]

But how would I do it using integers?
Obviously, s[c(1,2)] wouldn't work because if the set of number changes, it doesn't hold anymore.

Help would be appreiciated!


RE: Print out all odd numbers in random set of numbers ? - SIM-3364521 - Feb 6, 2019

in java:
int[] arr = new int[]{1,5,4,2};

for(int i=0; i<arr.length; i++){
System.out.print(arr[i] * (arr[i]%2));
}

you cant make decisions without using booleans so it will print 0 if the number was even.
not really sure if that is what you wanted but it will print all odd numbers and will print 0 if the number was even
you would have to use a boolean to filter out the even numbers completely.


RE: Print out all odd numbers in random set of numbers ? - V For Vendetta. - Feb 6, 2019

https://stackoverflow.com/questions/18950437/how-to-print-out-an-odd-integers-in-r


RE: Print out all odd numbers in random set of numbers ? - Talon - Feb 6, 2019

Code
public class oddInt
{
    public static void main(String[] args)
     {
          int[] array = new int[]{5,2,4,6,1,3, 9, 7 ,3, 23,43,1,2,34,5,6,7,10};
          for(int i = 0; i < array.length; i++)
          {
               if(array[i] % 2 != 0)
               {
                    System.out.println(array[i]);
               }
      }
}

}


Output:
5
1
3
9
7
3
23
43
1
5
7


RE: Print out all odd numbers in random set of numbers ? - Caliban - Feb 7, 2019

(Feb 6, 2019 08:27 PM)Tаlon Wrote:  
Code
public class oddInt
{
    public static void main(String[] args)
    {
         int[] array = new int[]{5,2,4,6,1,3, 9, 7 ,3, 23,43,1,2,34,5,6,7,10};
         for(int i = 0; i < array.length; i++)
         {
              if(array[i] % 2 != 0)
              {
                   System.out.println(array[i]);
              }
     }
}

}


Output:
5
1
3
9
7
3
23
43
1
5
7

You can also use this since Java 8:
Code
public class OddInt {
   public static void main(String[] args) {
       int[] array = {5, 2, 4, 6, 1, 3, 9, 7, 3, 23, 43, 1, 2, 34, 5, 6, 7, 10};
       Arrays.stream(array).filter(x -> (x % 2 != 0)).forEach(System.out::println);
   }
}

Looks more sexy to me to do it in a functional style, and streaming can also boost performance