Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%
1. Write a program, using a function, that will check a single integer to determine if it is prime
number or not.
Enter a positive integer enter to check:
X is not a prime number.
Or
X is a prime number.
expand button
Transcribed Image Text:1. Write a program, using a function, that will check a single integer to determine if it is prime number or not. Enter a positive integer enter to check: X is not a prime number. Or X is a prime number.
Expert Solution
Check Mark
Introduction

A number is said to be prime number if it has only two factors 1 and itself

Program

#include <iostream>

using namespace std;

bool isPrime(int n){
    int factors=0;      //variable to store number of factors
    
    for(int i=1;i<=n;i++){  //i from 1 to n
        if(n%i==0)          //if i exactly divides n
            factors++;      //i is factor of n so increment factors by 1
    }
    
    if(factors==2)      //if factors is 2
        return true;    //number is prime and return true
    else                //if factors is not 2
        return false;   //number is not prime and return false
}

int main()
{
    int x;
    cout<<"Enter a positve integer enter to check: ";
    cin >> x;           //input a number
    
    if(isPrime(x)){     //call the function and it returns true
        cout << x << " is a prime number";      //print message saying number is prime
    }
    else{               //if function call returned false
        cout << x << " is not a prime number";  //print a message saying number is not prime
    }
    return 0;
}

bartleby

Step by stepSolved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education