Write a Program to Print Even and Odd Numbers in C Using For Loop
In this tutorial, we are going to see how to write a program to print even and odd numbers in C language using for loop. Considering we have an integer(N) and we need to print even and odd numbers from 0 to N using a C program.
There are four ways to check or print even and odd numbers in C, by using for loop, while loop, if-else, or by creating a function.
An even number is an integer exactly divisible by 2. Example: 0, 4, 8, etc.
An odd number is an integer that is not exactly divisible by 2. Example: 1, 3, 7, 15, etc.
Write a Program to Print Even and Odd Numbers in C Using For Loop
#include <stdio.h> void main() { int i,last; printf("Enter The Last Number : "); scanf("%d",&last); //For Loop //Code For Even Number List printf("\nEven Number List :\n "); for(i=2; i<=last; i=i+2){ printf(" %d",i); } //Code For Odd Number List printf("\nOdd Number List :\n "); for(i=1; i<=last; i=i+2){ printf(" %d",i); } }
Output:
Enter The Last Number : 10 Even Number List : 2 4 6 8 10 Odd Number List : 1 3 5 7 9