6-Write a program c++ to Print even and odd numbers between 10 numbers
- User
6-Write a program c++ to Print even and odd numbers between 10 numbers
- Assistant
Here is an example of a C++ program that prints even and odd numbers between a given range of numbers (10 in this case):
#include <iostream> int main() { int start = 1; // Initial number int end = 10; // Final number std::cout << "Even numbers between " << start << " and " << end << ":" << std::endl; for (int i = start; i <= end; i++) { if (i % 2 == 0) { std::cout << i << " "; } } std::cout << std::endl; std::cout << "Odd numbers between " << start << " and " << end << ":" << std::endl; for (int i = start; i <= end; i++) { if (i % 2 != 0) { std::cout << i << " "; } } return 0; }This program first defines the range of numbers between
startandend. It then uses aforloop to iterate through all the numbers in the range. Within the loop, it checks whether a number is even by using the modulo operator (%) to check if the number is divisible by 2. If the number is even, it is printed. Similarly, it checks for odd numbers by checking if the number is not divisible by 2. Finally, the program returns 0 to indicate the successful execution of the program.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.