Introduction
In this lab, you will learn how to use a while loop to find even numbers from a list of integers.
In this lab, you will learn how to use a while loop to find even numbers from a list of integers.
In this lab, you will learn how to find even numbers from a list using a while loop.
Create a file called even_numbers.c
and open it in WebIDE.
Copy and paste the following code into the even_numbers.c
file:
#include <stdio.h>
int main() {
int numbers[] = { 21, 78, 62, 90, 55, 10, 85, 45, 11, 2 };
int size = sizeof(numbers) / sizeof(numbers[0]);
int i = 0;
printf("The even numbers from the list are:\n\n");
while (i < size) {
if (numbers[i] % 2 == 0) {
printf("EVEN: %d\n", numbers[i]);
}
i++;
}
return 0;
}
Save the file.
Open a terminal and navigate to the directory where the even_numbers.c
file is saved.
Compile the code using the command gcc even_numbers.c -o even_numbers
.
Run the program by entering ./even_numbers
in the terminal.
After completing this lab, you will be able to use a while loop to find even numbers from a list of integers. The program will output the even numbers from the list on the terminal.