Odd printing program in C



This program is very simple and a good example of using conditional statement (if-else) in an iteration (i.e. for loop). We shall initiate a for loop having some finite iterations and check every value of it. Using if conditional statements we shall determine and print if the value is odd.

We can use % (mode) operator to find if the value is completely divisible by 2. If the value is not completely divisible by 2 it is odd, otherwise it is even.

Algorithm

Let's first see what should be the step-by-step procedure to compare two integers −

START
   Step 1 → Iterate value from 1 to 10
   Step 2 → Check if value is divisible by 2
   Step 3 → If false then display value
STOP

Pseudocode

Let's now see the pseudocode of this algorithm −

procedure odd_printing(A, B)

   FOR value 1 to 10 DO
      IF value%2 NOT EQUAL TO 0 THEN 
         DISPLAY value as even
      END IF
   END FOR

end procedure

Implementation

Now, we shall see the actual implementation of the program −

#include <stdio.h>

int main() {
   int i;

   for(i = 1; i <= 10; i++) {
      if(i%2 != 0)
         printf("%d\n", i);
   }
   return 0;
}

Output

Output of this program should be −

  1
  3
  5
  7
  9
loop_examples_in_c.htm
Advertisements