This is a C++ program that finds even and odd numbers between 1 and a given ending value. Let's go through the program step-by-step:
Overall, the program correctly identifies even and odd numbers in the given range and prints them to the console. However, there is some code duplication because the program uses two separate while loops to check even and odd numbers. The program could instead use a single while loop and check both even and odd numbers in one iteration. Additionally, the program assumes that the user will enter a positive integer for the ending value, so it may not work correctly for negative numbers or non-integer inputs. It's a good practice to add error checking to ensure that the program behaves correctly for all possible inputs.
#include<iostream> using namespace std; int main() { int i=1,n,even=0,odd=0; cout<<"\nEnter the Ending value:"; cin>>n; cout<<"\nEven numbers:"; while(i<=n) { if(i%2==0) { cout<<"\n"<<i; even++; } i++; } cout<<"\nOdd numbers:"; i=1; while(i<=n) { if(i%2==1) { cout<<"\n"<<i; odd++; } i++; } cout<<"\nTotal even numbers:"<<even; cout<<"\nTOtal odd numbers:"<<odd; return 0; }To download raw file Click Here
Enter the Ending value:20 Even numbers: 2 4 6 8 10 12 14 16 18 20 Odd numbers: 1 3 5 7 9 11 13 15 17 19 Total even numbers:10 TOtal odd numbers:10
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions