Printing Even and Odd Numbers using While Loops in C


This is a C program that prompts the user to enter an integer, 'n', and then uses two while loops to find and print all the even and odd numbers between 1 and 'n' (inclusive).

  • The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program. The int main() function is the starting point of the program execution. Inside the main function, several integers are declared. printf() is used to display the prompts for the user to enter the ending value, and scanf() is used to get the input and store it in the variable 'n'.
  • The first while loop starts with the initial value of 'i' as 1 and execute the code inside the loop as long as the condition i<=n is true. The first time the code is executed, the current value of 'i' is checked for evenness using if(i%2==0). If the condition is true, the current value of 'i' is printed using printf("\n%d",i) and the variable 'even' is incremented by 1. Then the value of 'i' is incremented by 1.
  • Then the condition i<=n is checked, if it's true the control jumps back to the start of the loop and the same process is repeated until the condition is false. Then, the same process is repeated in the second while loop, this time checking for oddness using if(i%2==1) instead of evenness and incrementing the variable 'odd' instead of 'even' if the condition is true.
  • Finally, the program prints the total number of even and odd numbers using printf("\nTotal even numbers:%d",even); and printf("\nTOtal odd numbers:%d",odd); respectively and returns 0 to indicate the successful execution of the program.

While loops are a control structure that allows you to repeat a block of code as long as a certain condition is true. They are a more structured and readable alternative to using goto statement. The modulus operator (%) is used to get the remainder of a division of two numbers. In this case, it's used to check if the current value of 'i' is even (i%2==0) or odd (i%2==1).

Source Code

#include<stdio.h>
int main()
{
  int i=1,n,even=0,odd=0;
   printf("\nEnter the Ending value:");
   scanf("%d",&n);
   printf("\nEven numbers:");
   while(i<=n)
   {
      if(i%2==0)
      {
        printf("\n%d",i);
        even++;
      }
      i++;
   
   }
   printf("\nOdd numbers:");
   i=1;
   while(i<=n)
   {
     if(i%2==1)
     {
        printf("\n%d",i);
        odd++;
     }
     i++;
   }
   printf("\nTotal even numbers:%d",even);
   printf("\nTOtal odd numbers:%d",odd);
   return 0;
}
To download raw file Click Here

Output

Enter the Ending value:10
Even numbers:
2
4
6
8
10
Odd numbers:
1
3
5
7
9
Total even numbers:5
Total odd numbers:5


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C