Example
Input
Input upper limit: 10
Output
Odd numbers between 1 to 10: 1, 3, 5, 7, 9
/**
* C program to print all Odd numbers from 1 to n
*/
#include <stdio.h>
int main()
{
int i, n;
/* Input 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 increment it by 1 */
for(i=1; i<=n; i++)
{
/* If 'i' is odd then print it */
if(i%2!=0)
{
printf("%d\n", i);
}
}
return 0;
}
Logic to print odd numbers from 1 to n without if
statement
/**
* C program to display all odd numbers between 1 to n without using if statement
*/
#include <stdio.h>
int main()
{
int i, n;
/* Input 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;
}
/*
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
*/