Question
Q: I need to write a program that asks the user to input a positive integer n, and print all of the numbers from 1
Q: I need to write a program that asks the user to input a positive integer n, and print all of the numbers from 1 to n that have more even digits than odd digits.
For example, if n=30, the program should print:
2
4
6
8
20
22
24
26
28
currently my code looks like this:
#includeusing namespace std; int main() { int n, i; int even_count = 0; int odd_count = 0; cout << "Please enter a positive integer: "; cin >> n; for (i = 1; i < n; i++) { if(i % 2 == 0) even_count++; else odd_count++; } cout << even_count << endl; cout << odd_count << endl; }
I know that I need to check each number between 1 and n to see if the number has more even than odd numbers. if it does have more even numbers, then I need to print it to the console, if not then skip. Doing this all the way up to n. However, I just can't seem to figure it out.
As my code is right now. it will loop through every number between 1 and n, which I want to do, but instead of telling me how many odds and evens I have for each number between 1 and n it just tells me how many I have for n.
How do I check each number so that I can compare odds against evens? I thought If I add in a nested if statement like:
if (even_count > odd_count) { cout << i << endl; }
that it might do the trick, but I was mistaken.
I know there is a gap in my code I just cant get it! Any help is appreciated.
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started