Odd number
Opposite of even numbers, odd numbers are having a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.
Logic
The logic for printing odd numbers are pretty simple and straight forward, we only need to check if the number is not divisible by 2. If the number is not divisible by 2, then we have to print it.
Program
#include <iostream> using namespace std; int main() { int i, n; // Take input from user. cout << "Print all odd numbers till : "; cin >> n; cout << endl << "Odd numbers from 1 to " << n << " are : " << endl; for(i = 1; i <= n; i++) { // Check for odd or not. if((i % 2) != 0) { cout << i << " "; } } return 0; }
Output
Print all odd numbers till : 50
Odd numbers from 1 to 50 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