Write a C++ Program to Print Odd Numbers from 0 to given value. This C++ program allows users to enter any integer number. Next, we used the for loop to iterate numbers from 1 to user given value. Within the loop, we used If statement to check whether( i % 2 != 0 ). If true, print the i value as the odd number.
#include<iostream> using namespace std; int main() { int number; cout << "\nPlease Enter Maximum limit Value to print Odd Numbers = "; cin >> number; cout << "\nList of Odd Numbers from 1 to " << number << " are\n"; for(int i = 1; i <= number; i++) { if ( i % 2 != 0 ) { cout << i <<" "; } } return 0; }
Please Enter Maximum limit Value to print Odd Numbers = 10
List of Odd Numbers from 1 to 10 are
1 3 5 7 9
In this C++ Program to return Odd Numbers, we alter the for loop to remove the If statement. Here, we incremented the i value to 2 (instead of 1). So that every number from 1 with an increment of two will be odd numbers.
#include<iostream> using namespace std; int main() { int number; cout << "\nPlease Enter Maximum limit Value to print Odd Numbers = "; cin >> number; cout << "\nList of Odd Numbers from 1 to " << number << " are\n"; for(int i = 1; i <= number; i = i + 2) { cout << i <<" "; } return 0; }
Please Enter Maximum limit Value to print Odd Numbers = 24
List of Odd Numbers from 1 to 24 are
1 3 5 7 9 11 13 15 17 19 21 23
C++ Program to Print Odd Numbers using a While Loop
#include<iostream> using namespace std; int main() { int number, i = 1; cout << "\nPlease Enter Maximum limit Value to print Odd Numbers = "; cin >> number; cout << "\nList of Odd Numbers from 1 to " << number << " are\n"; while(i <= number) { cout << i <<" "; i = i + 2; } return 0; }
Please Enter Maximum limit Value to print Odd Numbers = 32
List of Odd Numbers from 1 to 32 are
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
This C++ print odd numbers example allows entering a minimum and maximum value. Next, it prints odd numbers from minimum to maximum.
#include<iostream> using namespace std; int main() { int minimumOdd, maximum; cout << "\nPlease Enter Minimum limit Value to print Odd Numbers = "; cin >> minimumOdd; cout << "\nPlease Enter Maximum limit Value to print Odd Numbers = "; cin >> maximum; cout << "\nList of Odd Numbers from " << minimumOdd << " to " << maximum << " are\n"; for(int i = minimumOdd; i <= maximum; i++) { if ( i % 2 != 0 ) { cout << i <<" "; } } return 0; }
The first if statement in this C++ print odd numbers check whether the minimum value percentage 2 is equal to 0. If True, it is an even number, so increment the minimum value by 1; otherwise, enter into the for loop.
#include<iostream> using namespace std; int main() { int minimumOdd, maximum; cout << "\nPlease Enter Minimum limit Value to print Odd Numbers = "; cin >> minimumOdd; cout << "\nPlease Enter Maximum limit Value to print Odd Numbers = "; cin >> maximum; if (minimumOdd % 2 == 0 ) { minimumOdd++; } cout << "\nList of Odd Numbers from " << minimumOdd << " to " << maximum << " are\n"; for(int i = minimumOdd; i <= maximum; i = i + 2) { cout << i <<" "; } return 0; }
Please Enter Minimum limit Value to print Odd Numbers = 50
Please Enter Maximum limit Value to print Odd Numbers = 80
List of Odd Numbers from 51 to 80 are
51 53 55 57 59 61 63 65 67 69 71 73 75 77 79