Create Even Numbers List in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to create a list of even numbers using a while loop in C. The lab covers the following steps: initializing a C project and file, declaring and initializing an integer array, implementing a while loop to find even numbers, printing the even numbers from the list, and compiling and running the C program. By the end of this lab, you will have a solid understanding of using while loops to generate even numbers in C.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/ControlFlowGroup(["Control Flow"]) c(("C")) -.-> c/CompoundTypesGroup(["Compound Types"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/BasicsGroup -.-> c/operators("Operators") c/ControlFlowGroup -.-> c/if_else("If...Else") c/ControlFlowGroup -.-> c/while_loop("While Loop") c/CompoundTypesGroup -.-> c/arrays("Arrays") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-438246{{"Create Even Numbers List in C"}} c/operators -.-> lab-438246{{"Create Even Numbers List in C"}} c/if_else -.-> lab-438246{{"Create Even Numbers List in C"}} c/while_loop -.-> lab-438246{{"Create Even Numbers List in C"}} c/arrays -.-> lab-438246{{"Create Even Numbers List in C"}} c/output -.-> lab-438246{{"Create Even Numbers List in C"}} end

Initialize C Project and File

In this step, you will set up the project environment and create a new C file for finding even numbers using a while loop.

  1. Open the terminal in WebIDE.

  2. Navigate to the project directory:

cd ~/project
  1. Create a new C source file named even_numbers.c:
touch even_numbers.c

Open the file in WebIDE, ready for you to start writing your C program to find even numbers using a while loop.

Declare and Initialize Integer Array

In this step, you will learn how to declare and initialize an integer array in C, which will serve as the source for finding even numbers.

  1. In the WebIDE, add the following code to your even_numbers.c file:
#include <stdio.h>

int main() {
    // Declare and initialize an integer array
    int numbers[] = {21, 78, 62, 90, 55, 10, 85, 45, 11, 2};

    // Calculate the size of the array
    int size = sizeof(numbers) / sizeof(numbers[0]);
}
  1. Let's break down the array declaration:

    • int numbers[] declares an integer array
    • {21, 78, 62, 90, 55, 10, 85, 45, 11, 2} initializes the array with specific integer values
    • sizeof(numbers) / sizeof(numbers[0]) calculates the total number of elements in the array
  2. Add a print statement to verify the array size:

#include <stdio.h>

int main() {
    int numbers[] = {21, 78, 62, 90, 55, 10, 85, 45, 11, 2};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Print the array size
    printf("Array size: %d\n", size);

    return 0;
}

Compile and run the program to check the array size.

gcc even_numbers.c -o even_numbers
./even_numbers

Example output:

Array size: 10

Implement While Loop to Find Even Numbers

In this step, you will learn how to use a while loop to iterate through the array and find even numbers.

  1. Update your even_numbers.c file with the following code to implement the while loop:
#include <stdio.h>

int main() {
    int numbers[] = {21, 78, 62, 90, 55, 10, 85, 45, 11, 2};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Initialize loop counter
    int i = 0;

    // Print header for even numbers
    printf("The even numbers from the list are:\n\n");

    // Implement while loop to find even numbers
    while (i < size) {
        // Check if number is even using modulo operator
        if (numbers[i] % 2 == 0) {
            printf("EVEN: %d\n", numbers[i]);
        }

        // Increment loop counter
        i++;
    }

    return 0;
}

Compile and run the program to find even numbers in the array.

gcc even_numbers.c -o even_numbers
./even_numbers

Example output:

The even numbers from the list are:

EVEN: 78
EVEN: 62
EVEN: 90
EVEN: 10
EVEN: 2
  1. Let's break down the while loop:

    • int i = 0 initializes the loop counter
    • while (i < size) continues the loop until all array elements are checked
    • numbers[i] % 2 == 0 checks if a number is even using the modulo operator
    • printf() prints even numbers
    • i++ increments the counter to move to the next array element
  2. Save the file and prepare for compilation in the next step.

Summary

In this lab, you learned how to set up a C project and file, declare and initialize an integer array, implement a while loop to find even numbers, and print the even numbers from the list. You practiced creating a new C project directory, writing C code to declare and calculate the size of an integer array, and using a while loop to iterate through the array and print the even numbers. The key learning points include working with C file creation, array manipulation, and loop logic to achieve the desired output of an even numbers list.