Here's a simple C program with output and proper explanation to generate even and odd numbers up to a specified limit using For Loop.
# include <stdio.h> # include <conio.h> void main() { int n, i ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThe odd numbers are :\n\n") ; for(i = 1 ; i <= n ; i = i + 2) printf("%d\t", i) ; printf("\n\nThe even numbers are :\n\n") ; for(i = 2 ; i <= n ; i = i + 2) printf("%d\t", i) ; getch() ; }
Output of above program -
Enter the limit : 10
The odd numbers are :
1 3 5 7 9
The even numbers are :
2 4 6 8 10
Explanation of above program -
The program is very simple to understand. First it asks the user to enter a limit "n" up to which even and odd numbers are needed. Then using two for loops (one for calculating even numbers and other for odd numbers) the program calculates and prints the even and odd numbers up to limit "n". Let's take a look at the working of both loops with the help of an example.
Suppose the value of limit n is 10. In this case, the program will calculate and print all even and odd numbers separately up to 10.
Calculation of odd numbers -
The first for loop does the calculation of odd numbers.
Notice the syntax of this loop. The starting value of "i" is set to 1 (first odd number)
and the loop terminating condition is "i <= n". But after each
iteration the value of i is incremented by 2. So the output of this loop
for n = 10 is - 1, 3 (1 + 2), 5 (3 + 2), 7 (5 + 2), 9 (7 + 2),
Calculation of even numbers -
The second for loop does the calculation of even numbers.
Again notice the syntax of this loop. The starting value of "i" is set to 2 (first even number)
and the loop terminating condition is "i <= n". But after each
iteration again the value of i is incremented by 2. So the output of this loop
for n = 10 is - 2, 4 (2 + 2), 6 (4 + 2), 8 (6 + 2), 10 (8 + 2),
how to find the even numbers from 2 to 20 using if else statement in c?
@Savita Kwati: Take a look at this post... http://cprogramming.language-tutorial.com/2012/08/c-program-to-find-even-numbers-up-to.html
Hey , I wanted to know how can i find even and odd numbers when i have upper and lower limit
Very Nice Post visit nice example also Even odd example in java
Visit nice example Even odd logic
Hey,
Grazie! Grazie! Grazie! Your blog is indeed quite interesting around It's all fundamental C Programming Tutorial.rest just follows it! agree with you on lot of points!
Getting memory violation error when I use string function "strlwr", what wrong in step 3, please help, I am new to c language
Very useful article, if I run into challenges along the way, I will share them here.
Kind Regards,
Yamini
Nice article . There is good coding example collection visit
Top coding program example