Built with Tutify.io

    AIStudyLabs

    Practical Examples: Find Even/Odd Numbers and More

    Practical Examples: Find Even/Odd Numbers and More

    Introduction

    In this lesson, we will explore practical programming examples using C language control structures, particularly focusing on identifying even and odd numbers. We will cover how to utilize conditional statements with if-else structures, implement loops for repeated actions, and write functions to encapsulate our logic effectively. By the end of this lesson, you will have hands-on experience that reinforces your understanding of these key programming concepts.

    Identifying Even and Odd Numbers

    To determine whether a number is even or odd, we can utilize the modulus operator % in conjunction with a conditional statement. An even number is divisible by 2 (i.e., number % 2 == 0), while an odd number is not.

    Example 1: Using if-else Statement

    Here's a simple program that prompts the user for a number and tells them whether it's even or odd:

    #include <stdio.h>
    
    int main() {
        int number;
    
        printf("Enter an integer: ");
        scanf("%d", &number);
    
        if (number % 2 == 0) {
            printf("%d is an even number.\n", number);
        } else {
            printf("%d is an odd number.\n", number);
        }
    
        return 0;
    }
    

    Explanation

    Example Output

    Enter an integer: 5
    5 is an odd number.
    

    Utilizing Loops for Repeated Actions

    Loops are essential in programming for executing a block of code multiple times. In this section, we’ll utilize both for and while loops to print out even and odd numbers from 1 to 20.

    Example 2: Using for Loop

    Here is how to implement this using a for loop:

    #include <stdio.h>
    
    int main() {
        printf("Even numbers from 1 to 20:\n");
        for (int i = 1; i <= 20; i++) {
            if (i % 2 == 0) {
                printf("%d \n", i);
            }
        }
    
        printf("Odd numbers from 1 to 20:\n");
        for (int i = 1; i <= 20; i++) {
            if (i % 2 != 0) {
                printf("%d \n", i);
            }
        }
    
        return 0;
    }
    

    Explanation

    Example Output

    Even numbers from 1 to 20:
    2
    4
    6
    8
    10
    12
    14
    16
    18
    20
    Odd numbers from 1 to 20:
    1
    3
    5
    7
    9
    11
    13
    15
    17
    19
    

    Using while Loops

    Next, we can achieve the same output using a while loop. Let's print the even and odd numbers from 1 to 20 using this structure:

    Example 3: Using while Loop

    #include <stdio.h>
    
    int main() {
        int i = 1;
        printf("Even numbers from 1 to 20:\n");
        while (i <= 20) {
            if (i % 2 == 0) {
                printf("%d \n", i);
            }
            i++;
        }
    
        i = 1; // Reset i to 1 for odd numbers
        printf("Odd numbers from 1 to 20:\n");
        while (i <= 20) {
            if (i % 2 != 0) {
                printf("%d \n", i);
            }
            i++;
        }
    
        return 0;
    }
    

    Explanation

    Example Output

    Even numbers from 1 to 20:
    2
    4
    6
    8
    10
    12
    14
    16
    18
    20
    Odd numbers from 1 to 20:
    1
    3
    5
    7
    9
    11
    13
    15
    17
    19
    

    Writing Functions to Encapsulate Logic

    To make our code more modular and reusable, we can define functions for checking even/odd numbers. Here’s an example:

    Example 4: Function to Check Even or Odd

    #include <stdio.h>
    
    void checkEvenOdd(int number) {
        if (number % 2 == 0) {
            printf("%d is an even number.\n", number);
        } else {
            printf("%d is an odd number.\n", number);
        }
    }
    
    int main() {
        for (int i = 1; i <= 10; i++) {
            checkEvenOdd(i);
        }
        return 0;
    }
    

    Explanation

    Example Output

    1 is an odd number.
    2 is an even number.
    3 is an odd number.
    4 is an even number.
    5 is an odd number.
    6 is an even number.
    7 is an odd number.
    8 is an even number.
    9 is an odd number.
    10 is an even number.
    

    Summary / Key Takeaways

    In this lesson on Practical Examples: Find Even/Odd Numbers and More, you have learned:

    By mastering these skills, you are well on your way to becoming a confident C coder!

    C Programming Fundamentals: From Zero to Confident C Coder — Control Structures: Conditions and Loops

    Introduction to Programming and C

    Variables and Data Types

    Control Structures: Conditions and Loops

    Functions in C

    Arrays and Strings

    Working with Pointers

    File Handling in C

    Structures and Unions in C

    Project Development and Implementation

    Advanced C Concepts and Best Practices

    Tutorial Not Found