java - How to print even numbers in ascending order and odd numbers in descending order without using collection -


input:

10 3 1 45 67 2 56 89 22 11 69 

output (what want):

2 22 56  89 69 67 45 11 3 1 

i want print numbers in ascending order , odd numbers in descending order without using collection because don't know collection . me here how can print odd number in descending order able ptint till number

code:

import java.util.arrays; import java.util.scanner;  class t {      public static void main(string[] args) {         // todo auto-generated method stub          scanner sc = new scanner(system.in);         system.out.println("enter number");         int n = sc.nextint();          int s[] = new int[n];         int i;         (i = 0; < n; i++) {             int e = sc.nextint();             s[i] = e;         }          arrays.sort(s);         (int j = 0; j < n; j++) {              if (s[j] % 2 == 0) {                 system.out.println(s[j]);             }         }     } } 

code:

import java.util.arrays; import java.util.scanner;  class t {      public static void main(string[] args) {         // todo auto-generated method stub          scanner sc = new scanner(system.in);         system.out.println("enter number");         int n = sc.nextint();          int s[] = new int[n];         (int = 0; < n; i++) {             int e = sc.nextint();             s[i] = e;         }          arrays.sort(s);         // in ascending         system.out.println("\neven numbers in ascending order:");         (int j = 0; j < n; j++) {              if (s[j] % 2 == 0) {                 system.out.print(s[j] + " ");             }         }          // odd in descending         system.out.println("\nodd numbers in descending order:");         for(int j = (n -1); j >= 0; j--) {             if (s[j] % 2 == 1) {                 system.out.print(s[j] + " ");             }         }     } } 

output:

enter number 10 3 1 45 67 2 56 89 22 11 69  numbers in ascending order: 2 22 56  odd numbers in descending order: 89 69 67 45 11 3 1  

explanation:

since array sorted (in ascending order), print out numbers first.
traverse array in opposite direction (desc order), , print out odd numbers.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -