Print Even Numbers From 1 to 100 in C

There are several methods for printing even integers in C between 1 and 100.

Approach 1: Using for Loop

Using a 'for' loop is one of the easiest ways to print even integers in C from 1 to 100. The code for this strategy is as follows:

In this program, the initial value of 'i' is 2 (the first even integer), increasing by 2 each time until it reaches 100. We will only publish even numbers as a result.

Approach 2: Using While loop

Utilizing a 'while' loop is an additional strategy. If additional requirements need to be added or the increment is not constant, this method is more adaptable. Here is the key:

Although the for loop achieves the same goal, this code gives the user more control over the loop's actions.

Approach 3: Using Recursion

Recursion is a problem-solving method where a function calls itself. Although there may be more effective approaches for this goal, it's an intriguing technique to illustrate recursive programming. Here is a recursive method in C to display even values between 1 and 100:

Recursion in this code terminates after 'n' surpasses 100, at which point the 'printEven' function is called repeatedly with an increasing even number.

Approach 4: Using an Array

Even numbers can be kept in an array and printed later. This method may be helpful if you need to manipulate or use even numbers later in your application. Here is how to go about it:

In this code, the even integers are computed, stored in an array, and printed by iterating over the array.

Approach 4: Using a do-While Loop

Another technique for printing even numbers is the 'do-while loop'. Before testing the loop condition, it ensures the code block has run at least once. This is how to apply it:

The do-while loop, used in this code to display even numbers, is useful in some circumstances, particularly when you want to ensure that the loop will be executed at least once.

Approach 6: Using Conditional Statements

If you want to determine whether a number is even and then print it appropriately, you can also use conditional statements like if. Here's an illustration:

The modulo operator ('%') is used in this code to determine whether a number is even. If there is zero after multiplying by 2, the result is even, and we publish it.

Approach 7: Using Bitwise Operation

If you're interested in a bit-level method, you can use bitwise operations to find even numbers. Here is how to go about it:

In this code, we use the bitwise AND ('&') operation to check if the least significant bit (LSB) is zero, which is true for even numbers.

Approach 8: Using a Switch Statement

While unconventional, you can also use a switch statement to print even numbers. However, this approach is not recommended for this specific task, as it's less readable and inefficient. Here's an example:

When i is divided by 2, the switch statement in this code examines the remainder. The even number is printed if the remainder is zero.

Conclusion

There are several ways to print even numbers in C from 1 to 100, each with benefits and applications of their own. Which strategy you choose to employ will depend on your individual requirements, coding tastes, and preferences. While some techniques are more effective than others, all these strategies will help you achieve your goals. Understanding these approaches can make you a more adaptable C programmer and enable you to select the best strategy for any circumstance.