Question

C++ Your goal is to ask the user for a number and to determine if that...

C++

Your goal is to ask the user for a number and to determine if that number is a prime number. Use a function to determine if the number is prime. Your program should have the following:

 The name of the program should be Assignment 6.

 3 comment lines (description of the program, author, and date).

 Ask the user for an integer. Store this result in a variable with an appropriate name. (1 point)

 Write a function that has an integer parameter and determines if that integer is a prime number. A prime number is divisible by itself and 1. This function should return true if the number is prime and false if the number is not prime. Give this function an appropriate name. (5 points)

 Call the function and save the result in a variable. (2 points)

 Display the result of whether the user entered number is prime or not prime. (2 points)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

// return true if n is prime

bool isPrime(int n)

{

    // if no is not prime

    if( n < 2 )

        return false;

   

    // 2 is the only even prime number

    if( n == 2 )

        return 2;

       

    // any even number except 2 is not prime

    if( n % 2 == 0 )

        return false;

       

    int i;

    for( i = 3 ; i < n ; i += 2 )

        // if n is divisible by 3

        if( n % i == 0 )

            return false;

   

    // f no is prime

    return true;

}

int main()

{

    int n;

    cout<<"Enter a number : ";

    cin>>n;

   

    if( isPrime(n) )

        cout<<"\n"<<n<<" is prime.";

    else

        cout<<"\n"<<n<<" is not prime.";

       

    return 0;

}

------------------------------------------------------------------------------------------------------------------------------------

Sample Output

Add a comment
Know the answer?
Add Answer to:
C++ Your goal is to ask the user for a number and to determine if that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ write a program: Your goal is to ask record the sales for 5 different...

    In C++ write a program: Your goal is to ask record the sales for 5 different types of salsa, the total sales, and the names of the highest and lowest selling products. Your program should have the following:  The name of the program should be Assignment7.  3 comment lines (description of the program, author, and date).  Create a string array that stores five different types of salsas: mild, medium, sweet, hot, and zesty. The salsa names should...

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • In C++ write a program; Your goal is to create a structure to store information about...

    In C++ write a program; Your goal is to create a structure to store information about a movie. Your program should have the following: -The name of the program should be Assignment8. -3 comment lines (description of the program, author, and date) -Write a program that uses a structure named MovieData to store the following information about a movie: (3 points) Title, Director, Year released, Running time (in minutes) The program should create 2 MovieData variables, store values in their...

  • Write a C function named: div() with one local variable: m to prompt the user to...

    Write a C function named: div() with one local variable: m to prompt the user to enter one integer for the local variable m. When you call the function, it should display one of the following lines: If m is divisible by 9 the function displays the message m is divisible by 9 If m is divisible by 3 and not divisible 9 (e.g. 24) the function displays the message m is divisible by 3 If m is neither divisible...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • You are to write a program which will ask the user for number of students (dynamic...

    You are to write a program which will ask the user for number of students (dynamic array of class Student). For each student, you will ask for first name and number of grades (dynamic array of grades as a variable in Student). **The program should give the student random grades from 0-100** Find the averages of each student and display the information. **Display name, grades, and average neatly in a function in the Student class called Display()** Sort the students...

  • You are to write a program which will ask the user for number of students (dynamic...

    You are to write a program which will ask the user for number of students (dynamic array of class Student). For each student, you will ask for first name and number of grades (dynamic array of grades as a variable in Student). **The program should give the student random grades from 0-100** Find the averages of each student and display the information. **Display name, grades, and average neatly in a function in the Student class called Display()** Sort the students...

  • Guess the number! You will create a program that will ask the user to guess a...

    Guess the number! You will create a program that will ask the user to guess a number between 1 and 10. The pseudocode is below. Be sure to import random at the beginning of your code and use a comment block explaining what your program does #Guess the number week 4 #Name: #Date: #Random number, loop while true #ask user for number. #if number is too high or too low, tell user, if they guessed it break out of loop...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT