Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Prime.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- // The task is to create a function called isPrime, which accepts an integer as parameter and returns 1 if it is a prime number and 0 it is not
- //also there should be a program reading an iteger from the user which shows how many numbers to be entered
- //the function has to determine if the entered number is integer
- //if so it should output yes otherwise no
- #include <iostream>
- using namespace std;
- bool isPrime(int x) {
- // In case number is 1 or 2, return true
- if (x == 1 || x == 2)
- return true;
- // If it's divisible by i, then not prime
- for (int i = 2; i <= x / 2; i++) {
- if (x % i == 0)
- return false;
- }
- // If loop does finish and we get to here, then it's prime.
- return true;
- }
- int main()
- {
- int n, a;
- //to check for positive number because it cannot loop negative number
- do {
- cout << "Enter number of repetition " << endl;
- cin >> n;
- } while (n < 0);
- //loops how many numbers it will check
- while (n > 0)
- {
- cout << "Enter a number to check: " << endl;
- cin >> a;
- // to call the function and and check if entered number is prime
- if (isPrime(a) == true)
- cout << "Yes" << endl;
- else
- cout << "No" << endl;
- n--;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement