In this program, we are going to find out and print even or odd numbers using while loop. We have already covered another similar example for even or odd numbers.
While loop, input output statements, if else statement
We will take input from user through command prompt and use it as limit. Limit means range from 0 to input number provided so that we will find even odds from 0 to limit.
Next, we will check whether number is even value by doing modulo division of number by 2. Counter variable i will represent input from 0 to limit and will be modulo divided by 2 and if result is zero i.e. we get no remainder from operation then number is even else it is odd. we will put this condition in if statement condition and when body of it statement executes then printf statement inside it will echo message that number is even.
When if statement condition fails then else statement body will print message that number is odd. After closing if else body, counter variable i is incremented so that loop iterates till maximum number.
int N, i=0;
printf("\n Provide maximum limit:");
printf("\n even numbers: %d",i);
printf("\t odd numbers: %d",i);
Provide maximum limit:10
even numbers: 0 odd numbers: 1
even numbers: 2 odd numbers: 3
even numbers: 4 odd numbers: 5
even numbers: 6 odd numbers: 7
even numbers: 8 odd numbers: 9
even numbers: 10