2. About Numbers You need to make a program that: • asks a user to type in a positive number greater than 0, • checks if the number provided really is greater than 0, less than 0 (invaldi), or equal to 0 (stop program), • runs some tests on that number and reports results to the user • repeats the entire process until the user types in a 0. What tests? In your last lab you provided the reciprocal and log₂ values for the user supplied number. This week you'll do some additional tests: • reports whether the number is even or odd • reports whether the number is a multiple of 3 or not • reports wehther the number is prime or not

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 19PE
icon
Related questions
icon
Concept explainers
Question

bool isprime(long n) /* fixed from to https://www.geeksforgeeks.org/euclid-euler-theorem/?ref=lbp */
{
    // check whether a number is prime or not
    int i;
    for (i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
    return true;
}

2. About Numbers
You need to make a program that:
• asks a user to type in a positive number greater than 0,
• checks if the number provided really is greater than 0, less than 0 (invaldi), or equal to 0 (stop
program),
• runs some tests on that number and reports results to the user
repeats the entire process until the user types in a 0.
What tests? In your last lab you provided the reciprocal and log2 values for the user supplied number. This
week you'll do some additional tests:
reports whether the number is even or odd
reports whether the number is a multiple of 3 or not
reports wehther the number is prime or not
A sample run of the program might look like:
Enter a number greater than 0: 5
Number 5 is odd is not multiple of 3 is prime
Enter a number greater than 0: 6
Number 6 is even is multiple of 3 is not prime
Enter a number greater than 0: -4
Bogus: number must be > 0
Enter a number greater than 0: 253
Number 253 is odd is not multiple of 3 is not prime
Enter a number greater than 0:0
Bye.
The code to determine if a number is prime or not will be provided on NUoodle: you can cut and paste it
into your program.
3. Some Tips
A common mistake of novice programs is to think they have to build the entire program before trying it.
Don't try to build the whole program at once: do it in smaller steps that you can test. When one step it working,
add the next one.
For example, start with just the code that asks the user for input, reads in the number, then prints it out
to the user. You've already done that in Lab 2, so you could use the same code for that here. Compile and
test that.
When that works, add the code to make the loop: keep asking the user for a number until they type in 0,
then stop (use the break instruction). You could also add the code testing for a value < 0 and printing
error message (and using the continue instruction. Compile and test that.
When that works, add code for tests of the value; try just the even/odd first. Compile and test that.
And so on, adding small bits at a time to code you know already works.
Transcribed Image Text:2. About Numbers You need to make a program that: • asks a user to type in a positive number greater than 0, • checks if the number provided really is greater than 0, less than 0 (invaldi), or equal to 0 (stop program), • runs some tests on that number and reports results to the user repeats the entire process until the user types in a 0. What tests? In your last lab you provided the reciprocal and log2 values for the user supplied number. This week you'll do some additional tests: reports whether the number is even or odd reports whether the number is a multiple of 3 or not reports wehther the number is prime or not A sample run of the program might look like: Enter a number greater than 0: 5 Number 5 is odd is not multiple of 3 is prime Enter a number greater than 0: 6 Number 6 is even is multiple of 3 is not prime Enter a number greater than 0: -4 Bogus: number must be > 0 Enter a number greater than 0: 253 Number 253 is odd is not multiple of 3 is not prime Enter a number greater than 0:0 Bye. The code to determine if a number is prime or not will be provided on NUoodle: you can cut and paste it into your program. 3. Some Tips A common mistake of novice programs is to think they have to build the entire program before trying it. Don't try to build the whole program at once: do it in smaller steps that you can test. When one step it working, add the next one. For example, start with just the code that asks the user for input, reads in the number, then prints it out to the user. You've already done that in Lab 2, so you could use the same code for that here. Compile and test that. When that works, add the code to make the loop: keep asking the user for a number until they type in 0, then stop (use the break instruction). You could also add the code testing for a value < 0 and printing error message (and using the continue instruction. Compile and test that. When that works, add code for tests of the value; try just the even/odd first. Compile and test that. And so on, adding small bits at a time to code you know already works.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning