C Programming Examples
-
C Program to Check Whether a Number is Even or Odd
Welcome to this comprehensive blog post where we will explore how to write a C program that can print even and odd numbers using a for loop. Whether you are new to programming or already familiar with the concept, this post will provide you with easy-to-understand explanations, practical examples, and tips to help you master this programming task. So let’s dive right in!
Introduction
As a beginner in the world of programming, it’s important to start with simple programs to grasp the fundamentals. Printing even and odd numbers is a great exercise to understand the concepts of loops, conditionals, and basic arithmetic operations in C. By the end of this post, you’ll be able to write your own program that can identify and display even and odd numbers with ease.
What is a For Loop?
Before we dive into writing the program, let’s briefly understand what a for loop is. In programming, a loop is a control structure that allows us to repeat a set of statements multiple times. In addition, a for loop is one type of loop that provides a compact way to iterate over a range of values.
The general syntax of a for loop in C is as follows:
for (initialization; condition; increment/decrement) { // Statements to be executed in each iteration }
Here’s how it works:
The initialization part is where you set the initial value for the loop counter.
The condition part is evaluated before each iteration to determine whether the loop should continue or terminate.
The increment/decrement part is responsible for updating the loop counter after each iteration.
Now that we have a basic understanding of for loops, let’s proceed to writing our program.
Writing the Program
To print even and odd numbers using a for loop, we need to:
Set a range of numbers.
Check if each number in the range is even or odd.
Print the corresponding result.
Let’s break down these steps and write the program together.
Step 1: Setting the Range
First, we need to decide on the range of numbers we want to work with. For the sake of simplicity, let’s choose a range from 1 to 20.
#include <stdio.h> int main() { int start = 1; int end = 20; // Rest of the code goes here return 0; }
In the code above, we have declared two variables
start
andend
to store the starting and ending numbers of our range, respectively. Feel free to modify these values according to your requirements.Step 2: Checking for Even and Odd Numbers
Now that we have our range set, we can move on to checking whether each number is even or odd. In C, we can use the modulo operator
%
to determine the remainder of a division operation. If a number divided by 2 leaves a remainder of 0, it is even; otherwise, it is odd.#include <stdio.h> int main() { int start = 1; int end = 20; for (int num = start; num <= end; num++) { if (num % 2 == 0) { printf("%d is even\n", num); } else { printf("%d is odd\n", num); } } return 0; }
In the code above, we have added a for loop that iterates from the
start
number to theend
number. Inside the loop, we use anif-else
statement to check if each number is even or odd. If the conditionnum % 2 == 0
is true, we print that the number is even. Otherwise, we print that the number is odd.Step 3: Printing the Results
We have already incorporated the code to print whether a number is even or odd within the for loop. When you run the program, you will see the output displaying the results.
#include <stdio.h> int main() { int start = 1; int end = 20; for (int num = start; num <= end; num++) { if (num % 2 == 0) { printf("%d is even\n", num); } else { printf("%d is odd\n", num); } } return 0; }
Now that we have completed writing the program, let’s move on to the next section to see the program in action.
Program Output and Execution
In conclusion, to see the program in action, we need to compile and run it. Here’s how you can do that:
Open your preferred C compiler or integrated development environment (IDE).
Create a new C file and copy the program code into it.
Save the file with a
.c
extension, such aseven_odd.c
.Compile the code. In most IDEs, you can usually find a “Build” or “Run” option to compile the code.
If there are no errors, execute the program. You should see the output as follows:
1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even
Congratulations! You have successfully written a C program to print even and odd numbers using a for loop. This small program demonstrates the power and simplicity of loops in programming.
Further Exploration
Overall, now that you have grasped the concept of printing even and odd numbers using a for loop, there are several ways you can further explore and expand upon this knowledge. Here are some suggestions:
Modify the program to accept user input for the range of numbers.
Extend the program to find the sum of even and odd numbers separately.
Experiment with different ranges and analyze the patterns in the output.
Try implementing the same logic utilizing a while loop instead of a for loop.
Remember, practice is key when it comes to programming. The more you experiment and play around with the code, the better understanding you will develop.
Conclusion
In this blog post, we learned how to write a C program to print even and odd numbers using a for loop. We started by understanding the basics of loops and the syntax of a for loop in C. Then, we broke down the steps involved in writing the program and provided a complete code example. Finally, we saw the program in action and explored further possibilities for experimentation.
By working on this simple program, you have gained essential knowledge about loops, conditionals, and arithmetic operations in C. These fundamental concepts will serve as building blocks for more complex programming tasks in the future. So, continuously keep practicing, exploring, and pushing your boundaries as a programmer. Happy coding!