Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to generate the first 100 Odd Numbers using C#?
To generate the first 100 odd numbers in C#, you can use different approaches. The most straightforward method is to iterate through numbers and check if they are odd using the modulus operator.
Syntax
Following is the syntax for checking if a number is odd −
if (number % 2 != 0) {
// number is odd
}
Following is the syntax for generating odd numbers in a loop −
for (int i = 1; iUsing Loop with Modulus Check
This method iterates through numbers from 1 to 200 and checks if each number is odd using the modulus operator. When
val % 2 != 0, the number is odd −using System; class Program { static void Main() { int val; int count = 0; Console.WriteLine("First 100 odd numbers:"); for (val = 1; valThe output of the above code is −
First 100 odd numbers: 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 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199Using Direct Odd Number Generation
A more efficient approach is to generate odd numbers directly by starting from 1 and incrementing by 2 in each iteration −
using System; class Program { static void Main() { Console.WriteLine("First 100 odd numbers (optimized approach):"); for (int i = 1, count = 0; countThe output of the above code is −
First 100 odd numbers (optimized approach): 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 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199Using Array to Store Odd Numbers
You can also store the first 100 odd numbers in an array for further processing −
using System; class Program { static void Main() { int[] oddNumbers = new int[100]; // Generate first 100 odd numbers for (int i = 0; iThe output of the above code is −
First 10 odd numbers from array: 1 3 5 7 9 11 13 15 17 19 Last 10 odd numbers from array: 181 183 185 187 189 191 193 195 197 199Comparison
| Approach | Time Complexity | Description |
|---|---|---|
| Loop with Modulus Check | O(2n) | Checks every number for odd condition |
| Direct Generation | O(n) | Generates only odd numbers, more efficient |
| Array Formula | O(n) | Uses mathematical formula: 2*i + 1 |
Conclusion
Generating the first 100 odd numbers in C# can be accomplished using multiple approaches. The most efficient method is direct generation by incrementing by 2, or using the mathematical formula 2*i + 1 to calculate odd numbers directly without checking each number's divisibility.
