Read the elements of the sequence one by one using a loop and immediately print the odd ones. A number is considered odd if val \% 2 \ne 0. However, the condition val \% 2 == 1 is incorrect for checking the oddness of negative values of . In the C language, dividing a negative odd number by results in .
Algorithm implementation — loop
Read the number of input values .
scanf("%d",&n);Process the numbers sequentially in a loop.
for(i = 0; i < n; i++)
{
scanf("%d",&val);If the number is odd, print it.
//if (val % 2 == 1 || val % 2 == -1) printf("%d ",val);
if (val % 2 != 0) printf("%d ",val);
}Algorithm implementation — array
Declare an array.
int m[101];Read the input sequence of integers into the array .
scanf("%d",&n);
for (i = 0; i < n; i++)
scanf("%d", &m[i]);Iterate through the numbers in the array. If is odd, print it.
for (i = 0; i < n; i++)
if (m[i] % 2 != 0) printf("%d ", m[i]);
printf("\n");Java implementation
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);Read the number of input values .
int n = con.nextInt();Read the input sequence of integers.
for(int i = 0; i < n; i++)
{
int val = con.nextInt();If the number is odd, print it.
if (val % 2 != 0) System.out.print(val + " ");
}
System.out.println();
con.close();
}
}Python implementation
Read the input sequence of integers into the list .
n = int(input())
lst = list(map(int,input().split()))Process the numbers in the list using a loop.
for x in lst:If the number is odd, print it.
if x % 2 != 0:
print(x, end = " ")Python implementation — list
Read the input sequence of integers into the list .
n = int(input())
lst = list(map(int,input().split()))Process the numbers in the list using a loop.
for i in range(n):If the number is odd, print it.
if lst[i] % 2 != 0:
print(lst[i],end = " ")Python implementation — create a list
Read the input data.
n = int(input())
lst = list(map(int,input().split()))Create a list that contains only odd integers.
p = [x for x in lst if x % 2 != 0]Print the answer.
print(*p)