C program to print all odd numbers from 1 to n

Previous Program Next Program

Write a C program to print all odd numbers from 1 to 100 using for loop. How to display odd numbers from 1 to n using loop in C programming. C program to print odd numbers in a given range. Logic to print odd numbers in a given range.

Example

Input

Input upper limit: 10

Output

Odd numbers between 1 to 10:
1, 3, 5, 7, 9

Required knowledge

Basic C programming, If else, For loop

Logic to print odd numbers from 1 to n using if

As like even numbers program, we can solve this using many approaches. Here I will explain the two simple and easiest approach that every beginner implement. Let us learn the first approach. But before that here are two prerequisites -

Below is the step by step descriptive logic to print odd numbers from 1 to n.

  1. Read the upper limit to print odd number from user. Store it in some variable say N.
  2. Run a loop from 1 to N, increment the loop counter by 1. The loop structure should look like for(i=1; i<=N; i++).
  3. Inside the loop body check if current number i is odd. Which is if(i%2!=0) then, print the value of i.

Program to print odd numbers using if statement

/**
 * C program to print all Odd numbers from 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n;
    
    //Read the upper limit from user
    printf("Print odd numbers till: ");
    scanf("%d", &n);

    printf("All odd numbers from 1 to %d are: \n", n);

    /*
     * Start loop from 1 and increments it by 1
     */
    for(i=1; i<=n; i++)
    {
        /* If the number is odd then print it */
        if(i%2!=0)
        {
            printf("%d\n", i);
        }
    }

    return 0;
}

Observe the above program for a while. You will notice that, I am unintentionally iterating n times. Also the above method is not the optimal method for this program. Hence, let us discuss other method to print odd numbers.

Logic to print odd numbers from 1 to n without if statement

Below is the step by step descriptive logic to print odd numbers.

  1. Read the upper limit to print odd number from user. Store it in some variable say N.
  2. Run a loop from 1 to N, but this time increment it by 2 instead of 1. The loop structure should look like for(i=1; i<=N; i+=2).
  3. Inside the loop body print the value of i.

Program to display odd numbers without using if

/**
 * C program to display all odd numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n;
    
    //Read the upper limit from user
    printf("Print odd numbers till: ");
    scanf("%d", &n);

    printf("All odd numbers from 1 to %d are: \n", n);

    /*
     * Start a loop from 1, increment it by 2.
     * For each repetition prints the number.
     */
    for(i=1; i<=n; i+=2)
    {
        printf("%d\n", i);
    }

    return 0;
} 

Note: In place of i+=2. You can also use i = i + 2. Both convey the same meaning.

Output
Print odd numbers till: 100
All odd numbers from 1 to 100 are: 
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99

Finally let us print odd numbers in a given range. Below is the program to print all odd numbers in a given range.

Program to print odd numbers in given range

/**
 * C program to display all odd numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, start, end;
    
    //Read the lower and upper limit from user
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All odd numbers from %d to %d are: \n", start, end);

    // If start is not odd then make it odd
    if(start%2==0)
    {
        start++;
    }

    /*
     * Initialize loop from start, increment it by 2.
     * For each repetition print the number.
     */
    for(i=start; i<=end; i+=2)
    {
        printf("%d\n", i);
    }

    return 0;
} 
Output
Enter lower limit: 10
Enter upper limit: 20
All odd numbers from 10 to 20 are: 
11
13
15
17
19

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

2 comments:

  1. Just a quick question since I am new in C does i+=2 mean i=i+2

    ReplyDelete
    Replies
    1. @Gaurav yes both are same. You can use any of them. But since i+=2 is short and trendy its a good programming practice to use i+=2 in short expressions such as in loops.

      Delete