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
- We include the standard input-output library to use
printfandscanffunctions. - We declare an integer variable
numberfor user input. - Using the
if-elsestructure, we determine the parity of the inputted number and output the appropriate message.
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
- Two
forloops are utilized: one for even numbers and another for odd numbers. - The loops iterate from 1 to 20, checking conditions for evenness and oddness with the
ifstatement as discussed earlier.
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
- A single integer variable
iis initialized to 1 for bothwhileloops. The while loops will continue untiliexceeds 20, checking conditions inside to differentiate even from odd.
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
- We define a function
checkEvenOddthat takes an integer as an argument and determines if it's even or odd. - We then use a
forloop inmainto call this function for numbers 1 through 10.
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:
- Use of Conditional Statements: Using
if-elseblocks for decision-making in C programming. - Identifying Number Types: Check even and odd numbers utilizing the modulus operator.
- Effective Looping: Implementing both
forandwhileloops to perform repetitive tasks. - Modular Functions: Encapsulating logic into functions for better code organization and reuse.
- Hands-on Practice: Gained practical experience through multiple coding examples.
By mastering these skills, you are well on your way to becoming a confident C coder!