Question

2. Write a function to test primality Use the definition of prime number to guide your implementation of the following functi
c++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include<iostream>
#include<math.h>
using namespace std;

bool IsPrime(int n){
   int i,flag=0; //flag to check number having factors or not
   for(i=2;i<=sqrt(n);i++) //check each number upto sqrt(n)
       if(n%i==0){ //if number is factor then fkag set 1 and break the loop
           flag=1;
           break;
       }
   if((flag)||(n==1)) //loop was terminated at flag=1 that means not a prime
       return false;
   else              //if flag strill 0 then that was prime number
       return true;
}
int main(){
   int n;
   cout<<"Enter Number: ";
   cin>>n;   //input the number to test primality
   if(IsPrime(n))
       cout<<n<<" is prime"; //printing result
   else
       cout<<n<<" is not a prime";
   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
c++ 2. Write a function to test primality Use the definition of prime number to guide...
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
  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • C++ Programming Use the following function to create a program that determines whether (n ^ 2...

    C++ Programming Use the following function to create a program that determines whether (n ^ 2 + 1) is prime for each of the primes not exceeding 1000. bool check_prime(int input) { for(int i = 2; i <= sqrt(input); ++i) {    if(input % i == 0)    {        return false;    } } return true; }

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...

    Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above. firstPrime: is 2 secondPrime: the first prime number after 2 thirdPrime: the third prime number …. nth prime: the nth prime number Your program must be organized as follows: int main() { //prompt the user to enter n //read n from the...

  • 20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real l...

    20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real list that takes a list of integers and returns the same numbers converted to real. For example, if the input is [1,2,3], the output should be like [1.0,2.0,3.0 22. [5 points] Using foldr function, write a function duplist of type·a list 'a list that takes...

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

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
ADVERTISEMENT