C (programming language)

C program to print Even number’s from 1 to n without using if-statement

While the if statement is a common tool for conditional branching in C, it’s not the only way to achieve specific outcomes. This guide explores printing even numbers between 1 and n without using if statements, showcasing alternative approaches and their advantages.

1. Using the Modulo Operator (%)

The modulo operator % returns the remainder after dividing two integers. For any even number n, dividing it by 2 will leave no remainder (0). Therefore, we can utilize % to check if a number is even:

C

for (int i = 1; i <= n; i++) {
  if (i % 2 == 0) {
    printf("%d ", i);
  }
}

However, this approach still uses an if statement. To completely avoid it, we can exploit the fact that even numbers differ from odd numbers by a multiple of 2. We can start with the first even number (2) and increment by 2 each time to print subsequent even numbers:

C

for (int i = 2; i <= n; i += 2) {
  printf("%d ", i);
}

This code eliminates the if statement, achieving the desired outcome with improved efficiency due to fewer comparisons.

2. Utilizing Bitwise Operators

C provides powerful bitwise operators that manipulate individual bits within a number. For even numbers, the least significant bit (LSB) is always 0. We can leverage the bitwise AND operator (&) to check this characteristic:

C

for (int i = 1; i <= n; i++) {
  if ((i & 1) == 0) {
    printf("%d ", i);
  }
}

Similar to the modulo approach, this method uses an if statement. However, replacing it with the bitwise OR operator (|) for checking the opposite condition (LSB set to 1 for odd numbers) allows complete if statement avoidance:

C

for (int i = 1; i <= n; i++) {
  if ((i | 1) == 1) {
    printf("%d ", i);
  }
}

This alternative uses bitwise operations efficiently, but its logic might be less intuitive for beginners compared to the modulo approach.

3. Leveraging Ternary Operator (?)

The ternary operator (?) provides a concise alternative to simple if-else statements. It can be used to conditionally print even numbers:

C

for (int i = 1; i <= n; i++) {
  printf("%d ", (i % 2 == 0) ? i : 0);
}

This code replaces the if statement within the loop with a ternary expression. While shorter, it might be less readable than explicit if statements for some programmers.

Conclusion

Printing even numbers without if statements in C showcases various techniques with their own advantages and disadvantages. Choose the approach that suits your specific needs and programming style, considering factors like efficiency, readability, and personal preference. By understanding these alternatives, you can expand your coding toolkit and write more sophisticated programs in C.

Remember, the best approach may depend on the context and complexity of your specific program. Consider these methods as tools to add to your C programming arsenal and choose the one that best suits your needs.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button