C Program to print all odd numbers

Category: C Program

C Program to print all odd numbers using while loop or for loop with and without if statement. How to print odd numbers by using while or for loop with and without if statement. Logic to find odd numbers within the given range of natural numbers.

C Program to print all odd numbers from 1 to n by using for loop and if condition

#include <stdio.h>

void main()
{
    int i, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    for(i = 1; i <= n; i++)
    {
        if (i % 2 != 0)
            printf("%d\n", i);
    }
}

C Program to print all odd numbers from 1 to n by using for loop and without using if condition

#include <stdio.h>

void main()
{
    int i, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    for(i = 1; i <= n; i=i+2)
    {
        printf("%d\n", i);
    }
}

C Program to print all odd numbers from 1 to n by using while loop and using if condition

#include <stdio.h>

void main()
{
    int i=1, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    while (i <= n)
    {
        if (i % 2 != 0)
            printf("%d\n", i);
        i++;
    }
}

C Program to print all odd numbers from 1 to n by using while loop and without using if condition

#include <stdio.h>

void main()
{
    int i=1, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    while (i <= n)
    {
        printf("%d\n", i);
        i = i + 2;
    }
}

Output

Enter value of n: 10
1
3
5
7
9

Recommended Posts