45.2k views
3 votes
Write a program that takes 10 integers as input. The program places the even integers into an array called evenList, the odd integers into an array called oddList, and the negative integers into an array called negativeList. The program displays the contents of the three arrays after all the integers have been entered

2 Answers

2 votes

Final answer:

A Python program was provided for categorizing 10 integers into arrays for even, odd, and negative numbers, using a loop and conditional statements.

Step-by-step explanation:

A student has asked for a program that takes 10 integers as input and segregates them into three different arrays based on their properties: even numbers, odd numbers, and negative numbers. Here's a simple program in Python that accomplishes this:

evenList = []
oddList = []
negativeList = []

for i in range(10):
number = int(input("Enter an integer: "))
if number < 0:
negativeList.append(number)
if number % 2 == 0:
evenList.append(number)
else:
oddList.append(number)

print("Even numbers:", evenList)
print("Odd numbers:", oddList)
print("Negative numbers:", negativeList)

This program first initializes three empty arrays: evenList, oddList, and negativeList. It then uses a loop to receive 10 integers from the user, checks if they are negative, even, or odd, and appends them to the corresponding lists. Finally, it prints out the contents of the arrays.

User Jonny Asmar
by
5.7k points
0 votes

import java.util.Scanner;

public class Main{

public static void main(String [] args){

Scanner integers = new Scanner(System.in);

int a, countNegative = 0, countOdd = 0, countEven = 0;

int []arr = new int[10];

System.out.println("Enter 10 integers");

for(a = 0; a < arr.length; a++){

arr[a] = integers.nextInt();

if(arr[a] < 0)

countNegative++;

if(arr[a] % 2 == 0)

countEven++;

if(arr[a] % 2 == 1)

countOdd++;

}

int[] oddList = new int[countOdd];

int[] evenList = new int[countEven];

int[] negativeList = new int[countNegative];

int o, e, n, o1, e1, n1, i;

for(o = 0; o < arr.length; o++){

if(arr[o] % 2 == 1){

for(o1 = 0; o1 < countOdd-1; o1++){

oddList[o1] = arr[o];

}

}

else if(arr[0] % 2 == 0){

for(e1 = 0; e1 < countEven-1; e1++){

evenList[e1] = arr[o];

}

}

else{

for(n1 = 0; n1 < countNegative-1; n1++){

negativeList[n1] = arr[o];

}

}

}

for(i = 0; i < countOdd-1; i++){

System.out.println(oddList[i]);

}

for(i = 0; i < countNegative-1; i++){

System.out.println(negativeList[i]);

}

for(i = 0; i < countEven-1; i++){

System.out.println(evenList[i]);

}

}

}

User Alexey Ten
by
4.9k points