- What are odd numbers and how to identify them
- How to print first N odd numbers in C using for loop
- Two different methods to print odd numbers
- How the code works with step-by-step explanation
- Practice assignments to strengthen your skills
Introduction
In this tutorial, you'll learn how to write a simple C program that prints the first N odd numbers using a for loop.
This is a perfect beginner-level problem that helps you understand how loops and number sequences work in C.
What Are Odd Numbers?
Odd numbers are numbers that are not evenly divisible by 2 ā they leave a remainder of 1. Examples are: 1, 3, 5, 7, 9, 11... and so on. So, when we say "print the first N odd numbers," we just need to display the first N numbers from this sequence.
š” Key Point: Odd numbers can be identified by checking if a number is not divisible by 2. In C, we use the condition i % 2 != 0 to check if a number is odd.
How Does the Program Work?
We use a loop that starts from 1 and checks each number one by one. We check if the number is odd using i % 2 != 0. If it is odd, we print it and increase the count. We stop when we've printed N odd numbers.
C Program to Print First N Odd Numbers (Method 1 - Using Modulo Operator)
#include <stdio.h>
int main() {
int n, i, count = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("First %d Odd Numbers:\n", n);
for(i = 1; count < n; i++) {
if(i % 2 != 0) // If i is odd
{
printf("%d ", i);
count++;
}
}
return 0;
}
Sample Output
Enter the value of N: 5 First 5 Odd Numbers: 1 3 5 7 9
If you enter 10, the output will be:
Enter the value of N: 10 First 10 Odd Numbers: 1 3 5 7 9 11 13 15 17 19
Alternate Method Using Formula (More Efficient)
You can also generate odd numbers using a direct formula: 2 * i - 1.
This approach skips the odd/even check and directly gives you odd numbers.
#include <stdio.h>
int main() {
int n, i;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("First %d Odd Numbers:\n", n);
for(i = 1; i <= n; i++) {
printf("%d ", 2 * i - 1);
}
return 0;
}
Enter the value of N: 5 First 5 Odd Numbers: 1 3 5 7 9
š Note: The formula method 2 * i - 1 is faster and more efficient when you only need odd numbers. The modulo method is more flexible when you need to perform extra checks or logic inside the loop.
Program Explanation
This C program asks the user to enter a number N and then prints the first N odd numbers on the screen.
It uses a for loop to check each number starting from 1 and prints it only if it's odd.
A separate counter keeps track of how many odd numbers have been printed so far.
The loop continues until exactly N odd numbers are displayed.
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n, i, count = 0;declares three variables ānfor user input,ias the loop counter, andcountto track how many odd numbers have been printed. - Get User Input:
printf("Enter the value of N: ");prompts the user, andscanf("%d", &n);reads the value. - Print Heading:
printf("First %d Odd Numbers:\n", n);displays a heading. - For Loop:
for(i = 1; count < n; i++)runs the loop untilcountreachesn:- Initialization:
i = 1starts the loop counter at 1. - Condition:
count < nchecks if we have printed enough odd numbers. - Update:
i++increments the counter after each iteration.
- Initialization:
- Check for Odd Number:
if(i % 2 != 0)checks if the current number is odd. - Print and Count:
printf("%d ", i);prints the odd number, andcount++;increments the count. - Return:
return 0;indicates successful program execution.
Conclusion
This small program is a great way to practice using for loops and conditional logic in C.
Understanding how to work with number patterns like odd or even numbers builds a strong foundation for more complex logic ahead.
Try extending this code to print even numbers or calculate the sum of odd numbers!
š» Practice Exercise
Challenge: Modify the program to print the sum of the first N odd numbers.
š Click to Show Solution
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum = sum + (2 * i - 1);
}
printf("Sum of first %d odd numbers: %d\n", n, sum);
return 0;
}
/* Output for N=5: Sum of first 5 odd numbers: 25 */
Frequently Asked Questions
1. What is the difference between odd and even numbers?
Odd numbers leave a remainder of 1 when divided by 2 (like 1, 3, 5, etc.), whereas even numbers are divisible by 2 with no remainder (like 2, 4, 6, etc.).
2. Why use a counter variable in the first program?
We use a counter because we don't know how many numbers we'll need to check to find exactly N odd numbers. The counter helps us track how many valid odd numbers we've printed.
3. Which method is better: using modulo or formula?
The formula method 2 * i - 1 is more efficient and direct when you just need the first N odd numbers. However, the modulo method i % 2 != 0 is more flexible if you need to perform extra checks or logic while filtering numbers.
4. Can this logic be used for even numbers too?
Absolutely! You can tweak the condition to check i % 2 == 0 or use the formula 2 * i to generate even numbers instead of odd ones.
5. What happens if I enter N = 0?
If you enter N = 0, the loop condition count < n (0 < 0) is false, so the loop doesn't run and nothing is printed.
š” Tip: Try extending this code to print even numbers or calculate the sum of odd numbers to practice more!
Practice Assignments Using For Loop
Assignment Set 1: Basic For Loop Programs
- 1. Write a program in C using for loop to display the first N natural numbers.
- 2. Write a program in C to print ODD numbers from 1 to N using for loop.
- 3. Write a program in C to print EVEN numbers from 1 to N using for loop.
- 4. Write a program in C to print all uppercase alphabets using for loop.
- 5. Write a program in C to print all lowercase alphabets using for loop.
- 6. Write a program in C using for loop to read 10 numbers and find their sum and average.
- 7. Write a C program to read an integer and print its multiplication table using for loop.
- 8. C program to check entered number is ZERO, POSITIVE or NEGATIVE using for loop.
- 9. Write a program in C to find factorial of a number using for loop.
- 10. Write a C program to find sum and average of first N natural numbers.
- 11. C program to reverse a given number using for loop.
- 12. C program to check if a number is an Armstrong number using for loop.
- 13. C program to print Fibonacci series up to N using for loop.
- 14. C program to check if a number is prime using for loop.
- 15. C program to convert decimal number to binary using for loop.
Assignment Set 2: Advanced For Loop Programs
- 1. C program to find sum of squares of first N natural numbers: 1² + 2² + 3² + ... + N²
- 2. C program to find sum of first N natural numbers: 1 + 2 + 3 + ... + N
- 3. C program to find sum of series: 1/1! + 1/2! + 1/3! + ... + N/N!
- 4. C program to find sum of series: 1 + 1/2 + 1/3 + 1/4 + ... + 1/N
- 5. C program to find sum of series: 1 + 3²/3³ + 5²/5³ + 7²/7³ + ...