Employing a 'for' loop stands out as a straightforward method to display even numbers in the C programming language within the range of 1 to 100. The implementation of this approach is outlined below:
#include <stdio.h>
int main() {
for (int i = 2; i <= 100; i += 2) {
printf("%d ", i);
}
return 0;
}
In this code, the starting value of 'i' is set to 2, which represents the first even integer. It increments by 2 in each iteration until it hits 100. Only even numbers will be displayed as the output.
Approach 2: Using While loop
Employing a 'while' loop presents an alternative approach. In cases where extra conditions must be incorporated or the increment varies, this technique proves to be more flexible. Here lies the crux:
#include <stdio.h>
int main() {
int i = 2;
while (i <= 100) {
printf("%d ", i);
i += 2;
}
return 0;
}
While the for loop accomplishes a similar objective, this code provides the user with increased authority over the loop's operations.
Approach 3: Using Recursion
Recursion is an approach to problem-solving in which a function invokes itself. While there exist potentially more efficient strategies to achieve the same objective, recursion serves as a captivating method to demonstrate recursive programming. Below is a recursive function in C designed to exhibit even numbers within the range of 1 to 100:
#include <stdio.h>
void printEven(int n) {
if (n <= 100) {
printf("%d ", n);
printEven(n + 2);
}
}
int main() {
printEven(2);
return 0;
}
Recursion in this script halts once 'n' exceeds 100, triggering the 'printEven' function to be recursively invoked with progressively larger even numbers.
Approach 4: Using an Array
Even integers can be stored in an array for future retrieval and utilization. This approach proves beneficial when there is a requirement to perform operations or tasks involving even numbers at a later stage within your software. Below are the steps to accomplish this:
#include <stdio.h>\
int main() {
int evenNumbers[50]; // We need 50 slots for even numbers from 1 to 100
int index = 0;
for (int i = 2; i <= 100; i += 2) {
evenNumbers[index] = i;
index++;
}
// Printing the even numbers from the array
for (int i = 0; i < 50; i++) {
printf("%d ", evenNumbers[i]);
}
return 0;
}
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
An alternative method for displaying numbers that are divisible by 2 is through the 'do-while loop'. This loop guarantees execution of the code block at least once before checking the loop condition. Here is how to implement this approach:
#include <stdio.h>
int main() {
int i = 2;
do {
printf("%d ", i);
i += 2;
} while (i <= 100);
return 0;
}
The do-while loop, applied in this code to showcase even numbers, proves beneficial in certain situations, especially when guaranteeing the loop's execution at least once is essential.
Approach 6: Using Conditional Statements
If you need to verify if a number is even and then display the result accordingly, you can also employ conditional constructs such as if. Below is an example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
return 0;
}
The modulo operator ('%') is employed in this code snippet to ascertain if a number is even. If the output of multiplying by 2 results in zero, then the number is considered even, and it is displayed.
Approach 7: Using Bitwise Operation
If you're inclined towards a binary-level approach, bitwise operations can be employed to identify even numbers. Here is the process to achieve this:
#include <stdio.h>
int main() {
for (int i = 1; i <= 100; i++) {
if ((i & 1) == 0) {
printf("%d ", i);
}
}
return 0;
}
In this example, we utilize the bitwise AND ('&') operator to verify if the least significant bit (LSB) equals zero, indicating even numbers.
Approach 8: Using a Switch Statement
While unorthodox, an alternative method involves employing a switch statement to display even numbers. Nevertheless, this technique is discouraged for this particular scenario due to its decreased readability and inefficiency. The following is a demonstration:
#include <stdio.h>
int main() {
for (int i = 1; i <= 100; i++) {
switch (i % 2) {
Case 0:
printf("%d ", i);
break;
default:
break;
}
}
return 0;
}
When dividing the variable i by 2, the switch statement in this code evaluates the resulting remainder. If the remainder is zero, the code prints the even number.
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.