I need to write a java program that seperates he even and odd numbers in an integer array.
This is what I have so far: This part is perfect:
package Homework;
import java.util.*;
public class EvenOdd
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("Please enter 10 integers");
int [] a= new int[10];
Scanner sc = new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
System.out.print("The "+(i+1)+" integer = ");
a[i]= sc.nextInt();
}
System.out.println("\nThe resulting array");
for(int i=0;i<a.length;i++)
{
for(int j=1;j<a.length;j++)
{
int temp;
if(a[i]%2!=0 && a[j]%2==0 && j>i)
{
temp=a[j];
a[j]=a[i];
a[j]=temp;
break; //There seems to be some problem in this loop
}
}
System.out.println("The "+(i+1)+" integer = "+a[i]);
}
If you use %2
which is modulo, you should be able to find if numbers are even or odd as even numbers mod 2 will equal 0 and odd numbers mod 2 will equal 1.
Try this for sorting the array:
for(int i=0;i<count;i++)
{
if(a[i] %2 != 0){//even
int temp = a[i];
a[i--] = a[count--];
a[count+1] = temp;
}
}
Introduce two Integer type ArrayLists list1 and list2. Put even numbers into list1 and odd numbers into list2. Merge both into list1.
public class EvenOdd {
public static void main(String[] args) {
System.out.println("Please enter 10 integers");
int[] a = new int[10];
ArrayList<Integer> list1 = new ArrayList();
ArrayList<Integer> list2 = new ArrayList();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < a.length; i++) {
System.out.print("The " + (i + 1) + " integer = ");
a[i] = sc.nextInt();
if (a[i] % 2 == 0) {
list1.add(a[i]);
} else {
list2.add(a[i]);
}
}
list1.addAll(list2);
for (int b : list1) {
System.out.print(b + " ");
}
}
}
import java.util.*;
class main11
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the range");
int no=sc.nextInt();
int a[]=new int[no];
int i;
System.out.println("Enter the elements");
for(i=0;i<no;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Even nos are");
for(i=0;i<no;i++)
{
if(a[i]%2==0)
{
System.out.println(a[i]);
}
}
System.out.println("The odd nos are");
for(i=0;i<no;i++)
{
if(a[i]%2!=0)
{
System.out.println(a[i]);
}
}
}
}
I believe your swapping of elements is not working due to a typo:
temp=a[j];
a[j]=a[i];
a[i]=temp; //Should be a[i] and not a[j]
break;
Also you can reduce the number of iterations by making the following changes in your for
loops:
for(int i=0;i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++)
{
int temp;
if(a[i]%2!=0 && a[j]%2==0) //no need of the last condition
//To store even and odd element in 2 diifferent array Aand print it//
import java.util.*;
class main11
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the range");
int no=sc.nextInt();
int a[]=new int[no];
int i,j=0,k=0;
System.out.println("Enter the elements");
for(i=0;i<no;i++)
{
a[i]=sc.nextInt();
}
int even[]=new int[no];
int odd[]=new int[no];
System.out.println("Even nos are");
for(i=0;i<no;i++)
{
if(a[i]%2==0)
{
even[i]=a[i];//to store element in even array//
System.out.println(even[i]+" ");
}
}
System.out.println("The odd nos are");
for(i=0;i<no;i++)
{
if(a[i]%2!=0)
{
odd[i]=a[i];//to store element in odd array//
System.out.println(odd[i]+" ");
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int j=0,k=0,n;
Scanner scn = new Scanner(System.in);
System.out.println("Enter number of element you want in your array:");
n = scn.nextInt();
int array[] = new int[n];
System.out.println("Your array element is : ");
for (int i=0;i<n;i++){
array[i]= scn.nextInt();
}
int even[] = new int[n];
int odd[] = new int[n];
for (int i=0;i<n;i++){
if (array[i] % 2 != 0){
odd[j] = array[i];
j++;
}
else{
even[k] = array[i];
k++;
}
}
System.out.println("Odd Array");
if (j>1)
{
for (int i=0;i<j-1;i++){
System.out.println(odd[i]+",");
}
System.out.println(odd[j-1]);
}
else {
System.out.println("No number");
}
System.out.println(" ");
System.out.print("Even :");
System.out.println("Odd Array");
if (k>1)
{
for (int i=0;i<k-1;i++){
System.out.println(even[i]+",");
}
System.out.println(even[k-1]);
}
else {
System.out.println("No number");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.