In this post, we will learn how to print odd numbers between 1 to 100 using C Programming language.
Odd Numbers are the integers that always leave a remainder when divided by 2. These numbers are the integers with the form n = 2k + 1, where k is an integer.
We will be printing odd numbers using three different methods. The methods are as follows:
- Using While Loop
- Using For Loop
- Without Using If Statement
So, without further ado, let’s begin this tutorial.
C Program To Print Odd Numbers Between 1 to 100
// C Program To Print Odd Numbers Between 1 To 100 #include <stdio.h> int main(){ int i; printf("Odd Numbers Between 1 To 100 are: \n"); for (i = 1; i <= 100; ++i){ if (i % 2 != 0){ printf("%d\n", i); } } return 0; }
Output
Odd Numbers Between 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
How Does This Program Work ?
int i;
In this program, we have declared an int data type variable named i.
for (i = 1; i <= 100; ++i){
if (i % 2 != 0){
printf("%d\n", i);
}
}
For Loop will make sure that the number lies between 1 to 100. Then, we check whether the number is exactly divisible by 2 or not.
If the number is not exactly divisible by 2, then it means the number is an odd number. Then, we print the value of i.
After that the value of i is incremented by 1 and then the program re-checks whether it is divisible by 2 or not. If it’s not divisible, then our program will print it otherwise it will ignore it.
This process continues until i <= 100.
C Program To Print Odd Numbers Using While Loop
// C Program To Print Odd Numbers Using While Loop #include <stdio.h> int main(){ int i = 1, num; // Asking for Input printf("Enter the maximum value: "); scanf("%d", &num); printf("Odd Numbers from 1 to %d are: \n", num); while (num >= i){ printf("%d\t", i); i = i + 2; } return 0; }
Output
Enter the maximum value: 14
Odd Numbers from 1 to 14 are:
1 3 5 7 9 11 13
C Program To Print Odd Numbers Using For Loop
// C Program To Print Odd Numbers #include <stdio.h> int main(){ int i, num; // Asking for Input printf("Enter the maximum value: "); scanf("%d", &num); printf("Odd Numbers From 1 to %d are: \n", num); for (i = 1; i <= num; i++){ if (i % 2 != 0){ printf("%d\n", i); } } return 0; }
Output
Enter the maximum value: 20
Odd Numbers From 1 to 20 are:
1
3
5
7
9
11
13
15
17
19
C Program To Print Odd Numbers Without Using If Statement
// C Program To Print Odd Numbers Without If Statement #include <stdio.h> int main(){ int i, num; // Asking for Input printf("Enter the maximum number: "); scanf("%d", &num); printf("Odd Numbers From 1 to %d are: \n", num); for (i = 1; i <= num; i = i + 2){ printf("%d\t", i); } return 0; }
Output
Enter the maximum number: 25
Odd Numbers From 1 to 25 are:
1 3 5 7 9 11 13 15 17 19 21 23 25
C Program To Print Odd Numbers in a Given Range
// C Program To Print Odd Numbers in a Given Range #include <stdio.h> int main(){ int i, min, max; // Asking for Input printf("Enter the minimum value: "); scanf("%d", &min); printf("Enter the maximum value: "); scanf("%d", &max); if (min % 2 ==0){ min++; } printf("Odd Numbers from %d to %d are: \n", min, max); for (i = min; i <= max; i++){ if (i % 2 != 0){ printf("%d\t", i); } } return 0; }
Output
Enter the minimum value: 5
Enter the maximum value: 20
Odd Numbers from 5 to 20 are:
5 7 9 11 13 15 17 19
Conclusion
I hope after going through this post, you understand how to print odd numbers between 1 to 100 using C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be glad to help you.
Also Read: