Recall our programming problem and imagine the following steps in your plan. Which construct do you think is the most appropriate to implement "2.a. Check whether the number is even or not". 1. Ask the user for an ending number. 2. Loop through each number from 1 to the user-provided number. For every number: 2.a. Check whether the number is even or not 2.b. Display the even number.
Added by Debbie K.
Step 1
Let's think step by step. Show more…
Show all steps
Your feedback will help us improve your experience
Deepak Kumar and 82 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Assignment 4: Evens and Odds Write a program that will ask a user for how many numbers they would like to check. Then, using a for loop, prompt the user for a number and output if that number is even or odd. Continue doing this as many times as the user indicated. Once the loop ends, output how many even numbers were entered and how many odd numbers were entered. Hint: For a number to be even, when the number is divided by 2, there should be no remainder - so you will want to use the modulus (%) operator. Hint: You will need two count variables to keep track of odd and even numbers. Sample Run 1 How many numbers do you need to check? 5 Enter number: 20 20 is an even number. Enter number: 33 33 is an odd number. Enter number: 4 4 is an even number. Enter number: 77 77 is an odd number. Enter number: 8 8 is an even number. You entered 3 even numbers. You entered 2 odd numbers. Sample Run 2 How many numbers do you need to check? 3 Enter number: 10 10 is an even number. Enter number: 3 3 is an odd number. Enter number: 400 400 is an even number. You entered 2 even numbers. You entered 1 odd number. Benchmarks 1. Prompt the user to answer the question, "How many numbers do you need to check?" 2. Create and initialize two count variables - one for odd and one for even. 3. Based on the previous input, create a for loop that will run that exact number of times. 4. Prompt the user to "Enter number:" 5. If that number is even, output "[number] is an even number." 6. Update the even count variable. 7. Otherwise, if the number is odd, output "[number] is an odd number." 8. Update the odd count variable. 9. Output "You entered [number] even number(s)." 10. Output "You entered [number] odd number(s).
Deepak K.
Write a program that will ask a user for how many numbers they would like to check. Then, using a for loop, prompt the user for a number and output if that number is even or odd. Continue doing this as many times as the user indicated. Once the loop ends, output how many even numbers were entered and how many odd numbers were entered.
Liam H.
Write a complete C++ program that does the following: 1. Ask the user to input a number. 2. Display a message telling the user whether the number is evenly divisible by 2, 3, 4, 5, 6, 7, 8, or 9. 3. Use a loop of your choice to loop through the numbers 2 through 9 to test for divisibility. 4. Use the modulus operator.
Akash M.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD